Java 如何在android中发布Json服务器Db特定格式并获得响应
我需要发布一个特定格式的数据这里是我的格式Java 如何在android中发布Json服务器Db特定格式并获得响应,java,android,xml,Java,Android,Xml,我需要发布一个特定格式的数据这里是我的格式 { "Authentication": { "Username": "*****", "Password": "****" }, "File": { "Orders": [{ "Status":"", }] }, "OrderID":16, "Requ
{
"Authentication": {
"Username": "*****",
"Password": "****"
},
"File": {
"Orders": [{
"Status":"",
}]
},
"OrderID":16,
"RequestType": 6
}
如何以这种格式发布数据并获得响应。我尝试了这种方法
HttpClient httpClient = new DefaultHttpClient();
JSONObject object = new JSONObject();
object.put("Username", "******");
object.put("Password", "*******");
JSONObject jsonObject = new JSONObject();
jsonObject.put("Authentication", object);
jsonObject.put("OrderID", data);
jsonObject.put("RequestType", 6);
HttpPost postMethod = new HttpPost("@@@@@@@@@@@@@");
postMethod.setEntity(new StringEntity(jsonObject.toString()));
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(postMethod);
entity = response.getEntity();
response_value = EntityUtils.toString(entity).toString();
Log.e(TAG, response_value);
我不知道如何在其中添加file对象,如果您有任何想法,请帮助我,您只需将JSON字符串发布到服务器并指定正确的头
application/json
服务器接收的json格式应与发送的格式相同。使用toString()
将json对象转换为字符串
例如:
String jsonObjString = jsonObject.toString();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://thisisasampleurl.com");
HttpEntity entity = new ByteArrayEntity(jsonObjectString
.getBytes("UTF-8"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String responseString = EntityUtils.toString(response
.getEntity());
您的代码中已经有了所需的头 请对此问题添加更多说明。如何指定标题