Json upload via android GET

Asked

Viewed 924 times

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 object JSON url, although it is possible. If it is really that, why not encoding the string before sending?

  • 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?

  • I didn’t get to test it, but I believe URLEncoder.encode("{}", "UTF-8"); should solve the problem.

  • Ok, but {} is a generated expression of an empty Hashmap (size==0), if the Hashmap had a larger size would this encoding be worth?

  • Ah... I guess you can’t use the HashMap directly, because it will not generate JSON, its shape is {chave=valor, chave=valor}. Take a look at the JSONObject and do the encoding just on toString of an instance of it and not in the entire URL.

  • It worked, thanks I did as follows. json = Urlencoder.Ncode(json); json = Sync.GET(context, address + json);

  • I’m gonna run some more tests.

  • When finished, create an answer with the solution. You can help others when they visit your question.

  • You can answer You solved, the method you told me worked.

Show 4 more comments

1 answer

2


In general the URI's are defined by RFC 3986 (Section 2 - Characters) and may contain the following characters::

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=

The others need to be encoded in the format

%hh

And how the format JSON has the character { and }, which are invalid, I recommend using the method URLEncoder.encode:

JSONObject objetoJSON = new JSONObject();

// Popula seu json com as propriedades

String url = caminho.concat(URLEncoder.encode(objetoJSON.toString(), "UTF-8"));

And instead of having something like:

http://10.0.3.2:8084/meuapp/recurso/objeto/download/{"chave1": "valor1", "chave2": "valor2"}

Will have something like:

http://10.0.3.2:8084/meuapp/recurso/objeto/download/%7B%22chave1%22%3A%20%22valor1%22%2C%20%22chave2%22%3A%20%22valor2%22%7D

References

  1. https://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid
  2. https://stackoverflow.com/questions/10786042/java-url-encoding
  • Thanks stayed on that forma----------------------> json = Urlencoder.Ncode(json,"UTF-8"); reply = Sync.GET(context, address + json)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.