Good afternoon, tell me I'm trying to send json for this I use this construction

JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "foo"); jsonObject.put("num", new Integer(100)); jsonObject.put("balance", new Double(1000.21)); jsonObject.put("is_vip", new Boolean(true)); StringWriter out = new StringWriter(); jsonObject.writeJSONString(out); HttpClient client = new DefaultHttpClient(); HttpPost http = new HttpPost(url); List nameValuePairs = new ArrayList(1); nameValuePairs.add(new BasicNameValuePair("type", out.toString())); http.setEntity(new UrlEncodedFormEntity(nameValuePairs)); String response = (String) client.execute(http, new BasicResponseHandler()); System.out.println(response); 

but the server writes

 {"error":{"id":3,"text":"no param name, no param num, no param balance, no param is_vip"}} 

what is the reason ?

    1 answer 1

    Try this:

      StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); HttpResponse response = new DefaultHttpClient().execute(httpPost); 
    • jsonBody is responsible for what? - Varg Sieg
    • Strange, it works for me - the body of the request contains the contents of json. JsonBody is your json converted to a string - Artem Konovalov
    • And maybe the reason is in the server? - Varg Sieg
    • @VargSieg I do not quite understand what the parameters are and how you are trying to pass them. And what role in all this plays json sent in the body of the request. - Artem Konovalov
    • Parameters of type String can be made POST request to the server without json. Just so that the server would accept them, I can not understand what my error is? - Varg Sieg