1

我对 Android 很陌生。所以,我在 android 中使用 Json 数据处理 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();

如何将 jsonObjUserCredential 包含为一个实体,例如:::

//StringEntity userCredential = new StringEntity(jsonObjUserCredential .toString(),HTTP.UTF_8);??????

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

请帮我....

4

2 回答 2

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);
于 2012-12-03T16:58:49.010 回答
1

尝试这样的事情:

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("myJSON", MyJSONObject.toString()));

//Set the http request 
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
httpPost = new HttpPost(webServiceUrl + methodName);

httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

//Variable to keep the http response
response = null;

//Set the parameters if exist
if(params != null && !params.isEmpty()){
    try {
        //Set the parameters in the request
    httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
}

//Execute the call
response = httpClient.execute(httpPost,localContext);

希望能帮助到你

于 2012-12-03T17:00:40.573 回答