在Android中发布基于webservice的请求

时间:2014-04-12 09:33:42

标签: android json web-services

我刚开始使用Web服务

 Method Type: POST

 Request Parameters :  {loginid:'xxxx@gmail.com', password:'yyyyy'}

我的尝试如下:

  JSONObject jObj = new JSONObject();
  try {
    jObj.put("loginid", email.getText());
    jObj.put("password", password.getText());
}

我该如何为它编写JSON代码。

1 个答案:

答案 0 :(得分:2)

JSONObject jObj = new JSONObject();
try {
    jObj.put("loginid", email.getText());
    jObj.put("password", password.getText());
}

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("www.yoursite.com/page");
StringEntity se = new StringEntity(jObj.toString());  
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
post.setEntity(se);
client.execute(post);