android - 使用 JSON Body 执行 HTTPPost 请求,Android

标签 android json post http-post getjson

我是 Android 的新手。因此,我在使用 Json 数据的 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);??????

这里做错了什么????

请帮帮我....

最佳答案

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

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

关于android - 使用 JSON Body 执行 HTTPPost 请求,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13687928/

相关文章:

php - 将其中包含引号的字符串作为不带反斜杠的 post 方法发送

android - 如何在 Activity 中同时捕获**视频和照片**

android - 如何捕获所需分辨率的视频?

android - ViewModelProviders 在 API 19 上找不到我的 ViewModel 类

java - android - 从 AsyncTask doInBackground() 线程更新 ListView

c# - 使用 Angular 发布将两个参数传递给 WEB API 调用

WCF WebInvoke具有查询字符串参数和一个帖子正文

javascript - 如何将 Ajax Json 响应附加到 html?

C# 不四舍五入

ios - NSURLConnection 顺序错误