0
Problem with special characters (keys, parentheses, etc) which contain in a json I’m sending via GET. android generates the following log:
> E/AndroidRuntime(1956): java.lang.IllegalArgumentException: Illegal
> character in path at index 61:
> http://10.0.3.2:8084/meuapp/recurso/objeto/download/{}
My method GET is as follows
public static String GET(Context context, String endereco){
//Verifica se existe conexão com a internet
if(!existeConexao(context))
return K.FALHA_CONEXAO;
InputStream inputStream = null;
HttpClient httpClient = new DefaultHttpClient();
String result = K.FALHA;
try{
HttpGet httpGet = new HttpGet(endereco);//A excessão acontece aqui
HttpResponse httpResponse = httpClient.execute(httpGet);
inputStream = httpResponse.getEntity().getContent();
if(inputStream!=null){
//Converte resposta do WebService para String
result = inputStreamParaString(inputStream);
}else
result = K.FALHA;
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
Why not send the object via
POST
? It is not common to send an objectJSON
url, although it is possible. If it is really that, why not encoding the string before sending?– Wakim
Why POST works, but I wanted to follow the GET standard ordering something and POST change something on the server. How can I encoding the String via GET?
– Skywalker
I didn’t get to test it, but I believe
URLEncoder.encode("{}", "UTF-8");
should solve the problem.– Wakim
Ok, but {} is a generated expression of an empty Hashmap (size==0), if the Hashmap had a larger size would this encoding be worth?
– Skywalker
Ah... I guess you can’t use the
HashMap
directly, because it will not generateJSON
, its shape is{chave=valor, chave=valor}
. Take a look at theJSONObject
and do the encoding just ontoString
of an instance of it and not in the entire URL.– Wakim
It worked, thanks I did as follows. json = Urlencoder.Ncode(json); json = Sync.GET(context, address + json);
– Skywalker
I’m gonna run some more tests.
– Skywalker
When finished, create an answer with the solution. You can help others when they visit your question.
– Wakim
You can answer You solved, the method you told me worked.
– Skywalker