使用JSON Body,Android执行HTTPPost请求

thu*_*rmi 1 post android json http-post getjson

我对Android很新.所以,我正在使用Json Data在Android中遇到HttpPost.

JSONObject jsonObj = new JSONObject();
JSONObject jsonObjDasUser = new JSONObject();

    jsonObj.put("name", "Login");
    jsonObj.put("type", "request");
    jsonObj.put("UserCredential", jsonObjUserCredential );
    jsonObjUserCredential .put("username", id);
    jsonObjUserCredential .put("password", password);

// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url); 

StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);

entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
Run Code Online (Sandbox Code Playgroud)

如何将jsonObjUserCredential包含为:::

//StringEntity userCredential = new StringEntity(jsonObjUserCredential .toString(),HTTP.UTF_8);??????
Run Code Online (Sandbox Code Playgroud)

这里做错了什么?????

请帮我....

San*_*ull 5

试试这个代码..希望这个解决你的问题..

JSONObject jsonObj = new JSONObject();
JSONObject jsonObjDasUser = new JSONObject();

jsonObj.put("name", "Login");
jsonObj.put("type", "request");
jsonObjUserCredential .put("username", id);
jsonObjUserCredential .put("password", password);
jsonObj.put("UserCredential", jsonObjUserCredential );

// Create the POST object and add the parameters

HttpPost httpPost = new HttpPost(url); 

StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
Run Code Online (Sandbox Code Playgroud)