1

我正在为 基于drupal的网站的 crate 应用程序工作。在这里,我面临从应用程序到 Web 服务器的发布页面的问题。

当我发布标题和正文时,只有标题出现在服务器上,但正文没有出现。这里我的正文部分变成了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("============@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+params);

当我打印参数的值时,我会像这样显示:

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

当我只通过身体部分时,它会给我状态码 406,而对于标题,它与200 状态码完美搭配

在这里我的问题是如何将字符串 + 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());

日志猫:

 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();      


 }
4

1 回答 1

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();      
    }
于 2012-10-06T10:30:52.067 回答