2011-05-11 92 views
10

我需要使用参数创建HTTP POST请求。我知道有很多例子,我尝试使用HTTPparams,NameValuePair等,但似乎无法得到正确的服务器格式。Android:参数不起作用的Http post

Server Type: REST based API utilizing JSON for data transfer
Content-type: application/json
Accept: application/json
Content-length: 47
{"username":"abcd","password":"1234"}

我可以通过这些信息,但是我似乎无法通过这些PARAMS“用户名”,“密码”。这里是我的代码:

HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
    pairs.add(new BasicNameValuePair("username","abcd")); 
    pairs.add(new BasicNameValuePair("password","1234")); 
    post.setHeader("Content-type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,"UTF-8"); 
    post.setEntity(entity); 
    HttpResponse response = client.execute(post); 

我试图调试,但不能看到,如果实体正确或不附...什么是我做错了什么?

在此先感谢。 玛斯

回答

15

试试这个:

HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
    post.setHeader("Content-type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
JSONObject obj = new JSONObject(); 
obj.put("username", "abcd"); 
obj.put("password", "1234"); 
    post.setEntity(new StringEntity(obj.toString(), "UTF-8")); 
    HttpResponse response = client.execute(post); 
+0

我需要的!非常感谢。 :) – Maaz 2011-05-11 02:30:05

+3

完美 - 添加,我喜欢使用定义的常量:'HTTP.UTF_8' – 2013-02-06 13:27:28

+0

为什么这个工作和OP的代码不?我面临类似的问题,但只有10个服务器中有1个。 – 2014-10-24 04:12:53

1

我不太清楚,从你的描述,但它似乎您的服务器需要一个JSON内容对象,而不是数据在URL中被编码。发送这样的事情作为您的帖子

{"username":"abcd","password":"1234"} 
+1

谢谢Dmon,Femis代码工作正常。但即使你的建议会导致我到正确的代码:) – Maaz 2011-05-11 02:26:30

1
HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 

pairs.add(new BasicNameValuePair("username","abcd")); 
pairs.add(new BasicNameValuePair("password","1234")); 

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8); 
post.setEntity(entity); 
HttpResponse response = client.execute(post); 

只是试试这个怎么把它的工作非常适合我,当我试图HTTP发布。

+0

谢谢苏曼特,我曾尝试在一个阶段,但没有工作。 :( – Maaz 2011-05-11 02:30:30

1

这可能会为你工作。

假设你已经有了JSON对象。

注:(1)在你需要处理的请求日为UTF-8(也在DB)服务器。

@SuppressWarnings("unchecked") 
private static HttpResponse executePostRequest(JSONObject jsonData, String url) { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpost = new HttpPost(url); 

    try { 
     httpost.setEntity(new ByteArrayEntity(jsonData.toString().getBytes("UTF8"))); 
     httpost.setHeader("Accept", "application/json"); 
     httpost.setHeader("Content-type", "application/json;charset=UTF-8"); 
     httpost.setHeader("Accept-Charset", "utf-8"); 
     HttpResponse httpResponse = httpclient.execute(httpost); 
     return httpResponse; 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

然后在客户端处理这样的服务器响应:

String responseBody = EntityUtils 
        .toString(response.getEntity(), "UTF-8");