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