当前位置: 代码迷 >> Android >> 无法通过发布请求发送JSON Raw
  详细解决方案

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

热度:86   发布时间:2023-08-04 12:01:20.0

这是我的POST 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;
    }

收到错误输出{“ response”:{“ code”:400,“ message”:“无效的Json”}}无法获得任何帮助,请帮助我

我验证了您提供的JSON,它是正确的。

我查看了您在POST上的编码,但是您是否通过或正在使用实际的JSON

?jsonObject.accumulate(“ origin_id”,“ 134”); jsonObject.accumulate(“ mobile_number”,“ 9888888888”);

但是,您的方法可以简化。 如果在EndPoints之前和之后插入log语句,它将查明哪些Java对象为您提供了哪些值。