我正在为箱子申请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();
}
[链接的问题(HTTP://计算器。 com/questions/3027066/how-to-send-a-json-object-over-request-with-android) –
4 **错误通常意味着你的应用程序被启动并发送了一些服务器不喜欢的答案可能在冒犯服务器日志 –
我有同样的问题是发布json字符串和演唱会json对象它有20个领域,我得到406个状态代码......如何处理 – venu