-2

我有一个示例 json 如下:

{ "Passwd":"字符串内容", "Userme":"字符串内容" }


如何构造上面的 JSON 字符串并将其作为 Android 中 HttpPost 的参数。?

谁能帮我解决这个问题。
提前致谢,

4

1 回答 1

0

您可以使用JSONObject创建一个简单的 json,例如{ "Passwd":"String content", "Userme":"String content" }尝试这样的事情。

            String json="";
            JSONObject jobj = new JSONObject();
            jobj.put("Userme", "Username");
            jobj.put("Passwd", "PasswordValue");
            json = jobj.toString();

上面的字符串可以使用 HTTP POST 作为参数之一轻松发送。下面的函数将 url 和 json 作为参数来发出 POST 请求。

private void httpPost(String json,String url) throws ClientProtocolException, IOException{
      HttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);
      List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
      nameValuePair.add(new BasicNameValuePair("json", json));
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
      httpClient.execute(httpPost);
  }
于 2012-07-04T18:18:22.970 回答