无法通过POST请求发送JSON RAW



这是我的帖子JSON,我必须通过Android应用程序传递数据,请帮助我

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

和我的Android代码

@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"}}无法任何人,请帮助我

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

我在帖子上查看您的编码,但是您是实际的JSON还是正在使用

〜jsonobject.accumulate(" origin_id"," 134"); jsonobject.accumulate(" Mobile_number"," 98888888888");

但是,您的方法可以简化。如果您在端点前后插入日志语句,它将指出Java对象携带哪些值为您的值。

最新更新