1

I am working for the crate application for the drupal based website.Here I face the problem for the post page from application to web server.

When I post the title and body then only title is appeared on server but body is not appeaerd.Here my body part into json format.

Below JSON format works perfectly into poster plugin of firefox,it successfully writes data to server, so now my question is to write android code for the same task.

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

I have tried like this:

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

when I print the value of the params then t display me like :

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

And when I pass only the body part then it gives me status code 406 and for title it works perfect with 200 status code

Here my questions is how to pass string + json both combine to server. Here title part is String variable and body part is json variableenter code here

Thanks.

EDIT:

  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 code

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


 }
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46
  • [Linked question](http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android) – Anup Cowkur Oct 06 '12 at 10:49
  • 4** errors usually mean that your application f**ked up and sent something server does not like - answer may be in offended server logs – Konstantin Pribluda Oct 06 '12 at 13:02
  • i have same issues am posting json string and concerting to json object it having 20 fields,am getting 406 status code...... how to handle it – venu Dec 10 '13 at 16:01

1 Answers1

1

Use built in JSON API's instead of Lists. In your case, traversing from innermost object to the outermost object, it would look like this:

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

Then, j1.toString(); would give you your required json string for output.

Then you can use a standard HTTP POST to send it to the server (using threads to keep network code off your UI thread) like this:

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();      
    }
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Thanks, But I have already done this,I got this values into `jSONCategory`, but my prblem how to pass this values to the server. – Rahul Patel Oct 06 '12 at 10:36
  • what was the error? print out j1.toString() and see if the string output is proper or not first – Anup Cowkur Oct 06 '12 at 11:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17643/discussion-between-anup-cowkur-and-rahul-patel) – Anup Cowkur Oct 06 '12 at 12:00