2017-02-16 129 views
0

这是帖子的Json我必须通过从Android应用程序的数据可以在任何一个请帮我无法通过POST请求发送JSON原始

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

+0

'{“prop”:“val”,}'无效json不是'[“a”,“b”,]' – Selvin

+0

检查您的Web服务服务器日志。这可能会揭示正在发生的事情。 – LoveForDroid

+0

@LoveForDroid显示的不是有效的Json –

回答

0

我验证了您给出的JSON,它是CORRE克拉。

我通过对POST的编码看,但是没有通过实际的JSON或正在使用

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

尽管如此,您的方法可以简化。如果您在EndPoints之前和之后插入日志语句,它将确定哪些java对象为您带来了什么值。