Java Json在android中发布特定格式的数据并获取响应
我需要以这种格式通过json url发布数据Java Json在android中发布特定格式的数据并获取响应,java,android,json,Java,Android,Json,我需要以这种格式通过json url发布数据 { "Authentication": { "Username": "testUser@123", "Password": "testPassword@123" }, "RequestType": 4 } 并获取android中特定请求的响应。如何做到这一点,如果您有想法,请帮助我尝试使用post方法将json发送到服务器 try { HttpClient httpClient = new DefaultHtt
{
"Authentication":
{
"Username": "testUser@123",
"Password": "testPassword@123"
},
"RequestType": 4
}
并获取android中特定请求的响应。如何做到这一点,如果您有想法,请帮助我尝试使用post方法将json发送到服务器
try {
HttpClient httpClient = new DefaultHttpClient();
JSONObject object = new JSONObject();
object.put("Username", "testUser@123");
object.put("Password", "testPassword@123");
JSONObject jsonObject = new JSONObject();
jsonObject.put("Authentication", object);
jsonObject.put("RequestType", 4);
HttpPost postMethod = new HttpPost("your_url");
postMethod.setEntity(new StringEntity(jsonObject.toString()));
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(postMethod);
HttpEntity entity = response.getEntity();
String response_value = EntityUtils.toString(entity).toString();
} catch (Exception e) {
e.printStackTrace();
}
你是说扩展名为.json的url?非常感谢。它工作得很好。