【问题标题】:Do a HTTPPost request with JSON Body, Android使用 JSON 正文执行 HTTPPost 请求,Android
【发布时间】:2012-12-03 16:50:40
【问题描述】:

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

这里有什么问题??????

请帮帮我....

【问题讨论】:

  • 表示您想将 Json 数据发布到我们的服务器,对吗? jsonObjUserCredential 是什么,您可以发布更多代码
  • jsonObjUserCredential 包含两个属性(用户名和密码)

标签: android json post http-post getjson


【解决方案1】:

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

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);

【讨论】:

  • 感谢您的代码。有关更多详细信息,我的 Json 看起来像这样... { "name":"Login" ,"type":"request" ,"UserCredential": { "id" : "xxxxx", "password": "xxxx" } } 如果我按照你所说的那样做......'d 那是一个很好的 JsonBody 来做 HttpPost 吗?谢谢老兄
  • 感谢 Sandeep .... 我可以看到相同的 JsonPost 已发布,但更改了顺序,这意味着:{"UserCredential":{"password":"xxxxx","id" :"xxxxx"},"type":"request","name":"Login"} 这应该不是问题吧?现在我遇到了这个问题.... bufferedreader 构造函数中使用的默认缓冲区大小如果需要 8k 字符缓冲区,最好明确我如何在我的 HttpPost 中修复它??
  • 如何在 ASP.Net MVC 服务器中查询 JSON 字符串?
  • 发帖请求不用加严吗?
【解决方案2】:

试试这样的:

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);

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多