2012-10-06 117 views
1

我正在为箱子申请drupal based网站。在这里,我面对从应用程序到网页服务器的帖子页面的问题。在Android中发布JSON数据到web服务器的错误?

当我发布标题和正文则只有标题出现在服务器,但身体我的身体部分没有appeaerd.Here到json格式。

下面的JSON格式完全适用于Firefox的海报插件,它成功地将数据写入服务器,所以现在我的问题是为同样的任务编写android代码。

{ 
"type":"page", 
"title":"TITLE TESTING", 
"body":{ 
    "und":[ 
    { 
    "value":"BODY TESTING" 
    } 
    ] 
} 
} 

我已经试过这样:

List<NameValuePair> params = new ArrayList<NameValuePair>(); 

    params.add(new BasicNameValuePair("type","page")); 
    params.add(new BasicNameValuePair("title",title)); 
    params.add(new BasicNameValuePair("body", ""+jSONCategory.toString())); 
// value of the jSONCategory.toString() : {"und":[{"value":"body_part"}]} 

System.out.println("[email protected]@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+params); 

当我打印的则params的值,则T显示我想:

[type=page, title=title_part, body={"und":{"value":"body_part","format":"full_html"}]}] 

而且当我通过只有身体部分,那么它给我状态代码406和标题它完美与200状态代码

这里我的问题是如何通过string + json两者结合到服务器。 这里标题部分是String variable和身体部位是json变量enter code here

谢谢。

编辑:

JSONCategory = new JSONObject(); 
    JSONBody = new JSONObject(); 

JSONObject Jsonvalue = new JSONObject(); 
        Jsonvalue.put("value", body); 

        JSONArray jsonUnd = new JSONArray(); 
        jsonUnd.put(Jsonvalue); 

        JSONCategory.put("und", jsonUnd); 

        JSONBody.put("type", "page"); 
        JSONBody.put("title", title); 
        JSONBody.put("body", JSONCategory); 

        System.out 
          .println("WHOLE JSON OBJECT ====================>" 
            + JSONBody.toString()); 

的logcat:

WHOLE JSON OBJECT ====================>{"type":"page","body":{"und":[{"value":"body"}]},"title":"title"} 

JAVA代码

@Override 
    public Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

     String url = "url here"; 

     // strResponse1= postData(url,title,JSONCategory); 
     postData(url, JSONBody); 

     System.out.println("=========> Response from post idea => " 
       + strResponse1); 

     return null; 
    } 
------------------------------------------------------------------------------- 

    protected void postData(final String url, final JSONObject mainJSON) { 


      Thread t = new Thread(){ 
      public void run() { 
        Looper.prepare(); //For Preparing Message Pool for the child Thread 
        HttpClient client = new DefaultHttpClient(); 
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
        HttpResponse response; 

        try{ 
         HttpPost post = new HttpPost(url); 

         StringEntity se = new StringEntity(mainJSON.toString()); 
         se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
         post.setEntity(se); 
         response = client.execute(post); 
         /*Checking response */ 
         if(response!=null){ 
          InputStream in = response.getEntity().getContent(); //Get the data in the entity 
         } 

        } 
        catch(Exception e){ 
         e.printStackTrace(); 
         //createDialog("Error", "Cannot Estabilish Connection"); 
        } 
        Looper.loop(); //Loop in the message queue 
       } 
      }; 
      t.start();  


} 
+0

[链接的问题(HTTP://计算器。 com/questions/3027066/how-to-send-a-json-object-over-request-with-android) –

+0

4 **错误通常意味着你的应用程序被启动并发送了一些服务器不喜欢的答案可能在冒犯服务器日志 –

+0

我有同样的问题是发布json字符串和演唱会json对象它有20个领域,我得到406个状态代码......如何处理 – venu

回答

1

使用内置在JSON API的,而不是名单。在你的情况,从最里面的对象到最穿越的对象,它应该是这样的:

JSONObject j4=new JSONObject(); 
j4.put("value",test_value); 

JSONArray j3=new JSONArray(); 
j3.put(0,j4); 

JSONObject j2=new JSONObject(); 
j2.put("und",j3); 

JSONObject j1=new JSONObject(); 
j1.put("type","page"); 
j1.put("title",title_string); 
j1.put("body",j2); 

然后,j1.toString();会给你的输出你需要的JSON字符串。

然后你可以使用标准的HTTP POST将其发送到服务器(使用线程,以保持网络代码把你的UI线程)是这样的:

protected void sendJson(JSONObject j1) { 
     Thread t = new Thread(){ 
     public void run() { 
       Looper.prepare(); //For Preparing Message Pool for the child Thread 
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 

       try{ 
        HttpPost post = new HttpPost(URL); 

        StringEntity se = new StringEntity(j1.toString()); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 
        /*Checking response */ 
        if(response!=null){ 
         InputStream in = response.getEntity().getContent(); //Get the data in the entity 

       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        createDialog("Error", "Cannot Estabilish Connection"); 
       } 
       Looper.loop(); //Loop in the message queue 
      } 
     }; 
     t.start();  
    } 
+0

谢谢,但我已经这样做了,我把这个值写入'jSONCategory',但是我的公关瑕疵如何将这个值传递给服务器。 –

+0

回答已更新,以反映您的问题。 –

+0

我试过上面的代码,但没有为我工作。 –

相关问题