我正在为基于 drupal 的网站的crate应用程序工作。我遇到了从应用程序到网络服务器的帖子页面的问题。
当我发布标题和正文时,只有标题出现在服务器上但身体没有上诉。我的身体部分是 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);
当我打印params的值时,然后显示我:
[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 :(得分:1)
使用内置的JSON API代替Lists。在您的情况下,从最内层对象遍历到最外层对象,它看起来像这样:
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();
}