Android java.lang.IllegalArgumentException in HttpGet

I am trying to get a response from a remote server. Here is my code:

private static String baseRequestUrl = "http://www.pappico.ru/promo.php?action=";

    @SuppressWarnings("deprecation")
    public String executeRequest(String url) {
        String res = "";
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;      

        try {   
            //url = URLEncoder.encode(url, "UTF-8");
            HttpGet httpGet = new HttpGet(url);

            response = httpClient.execute(httpGet);
            Log.d("MAPOFRUSSIA", response.getStatusLine().toString());

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream inStream = entity.getContent();
                res = streamToString(inStream);

                inStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res; 
    }

    public String registerUser(int userId, String userName) {
        String res = "";
        String request = baseRequestUrl + "RegisterUser&params={\"userId\":" +
                 userId + ",\"userName\":\"" + userName + "\"}";

        res = executeRequest(request);

        return res; 
    }

And I get the following exception on the line HttpGet httpGet = new HttpGet(url):

java.lang.IllegalArgumentException: Invalid character in query by index 59: http://www.pappico.ru/promo.php?action=RegisterUser¶ms= {"userId": 1, "userName": "Yuri Klinsky"}

What happened to the '{' character? I already read a few posts about this exception and found a solution, but this solution raises another exception: if I have not compromised the line url = URLEncoder.encode(url, "UTF-8");, it scrolls with the exception in the line response = httpClient.execute(httpGet);:

java.lang.IllegalStateException: . schem = null, host = null, path = http://www.pappico.ru/promo.php?action=RegisterUser¶ms= { "userId": 1, "userName": " + " }

, , . :)

+5
2

URL:

String request = baseRequestUrl + "RegisterUser&params=" +    
        java.net.URLEncoder.encode("{\"userId\":" + userId + ",\"userName\":\"" + userName + "\"}", "UTF-8");
+7

Try:

public String registerUser(int userId, String userName) {
        String res = "";

        String json = "{\"userId\":" +
                 userId + ",\"userName\":\"" + userName + "\"}";
        String encodedJson = URLEncoder.encode(json, "utf-8");

        String request = baseRequestUrl + "RegisterUser&params=" + encodedJson;

        res = executeRequest(request);
        return res;
    }

( URL- params =...), URL-. , .


Bonus: , JSON POST ( GET). ​​, "Live Headers", (, ), , . {..} . - HTTP POST JSON Java

, JSON ( ) , ObjectMapper (, Jackson), . , \" .

: JSON Java-, json

0

All Articles