我对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();
如何将jsonObjUserCredential包含为像:::
这样的实体//StringEntity userCredential = new StringEntity(jsonObjUserCredential .toString(),HTTP.UTF_8);??????
这里做错了什么?????
请帮助我......
答案 0 :(得分: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);
答案 1 :(得分: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);
希望有所帮助