无法通过发布请求发送JSON Raw

时间:2017-02-16 16:21:45

标签: android json http-post

这是我的POST Json我必须从Android应用程序传递数据可以任何人请帮助我

{  
   "book":{  
      "details":{  
         "detail":[  
            {  
               "title":"Ms"
            }
         ]
      },
      "contact_detail":{  
         "mobile_number":"9888888888"
      }
   },
   "origin_id":"134"
}

我的安卓代码

@Override
    protected Void doInBackground(Void... params) {
        HttpURLConnection urlConnection=null;
        String json = null;
        try {
            HttpResponse response;
            JSONObject jsonObject = new JSONObject();
             jsonObject.accumulate("origin_id", "134");
            jsonObject.accumulate("mobile_number", "9888888888");

            jsonObject.accumulate("title", "Ms");
            json = jsonObject.toString();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(json, "UTF-8"));
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.addHeader("Accept-Encoding", "gzip");
           // httpPost.setHeader("Accept-Language", "en-US");
            response = httpClient.execute(httpPost);
            String sresponse = response.getEntity().toString();
            Log.w("QueingSystem", json);
            Log.w("QueingSystem", sresponse);
            Log.w("QueingSystem", EntityUtils.toString(response.getEntity()));
        }
        catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());

        } finally {
    /* nothing to do here */
        }
        return null;
    }

获取错误输出 {"响应":{"代码":400,"消息":"不是有效的Json"}} 任何人都无法帮助我

1 个答案:

答案 0 :(得分:0)

我验证了你给出的JSON,这是正确的。

我在POST上查看你的编码,不过你是实际的JSON还是正在使用

  

~jsonObject.accumulate(" origin_id"," 134");               jsonObject.accumulate(" mobile_number"," 9888888888");

然而,您的方法可以简化。如果在EndPoints之前和之后插入日志语句,它将确定哪些java对象为您提供了哪些值。