Android:Http帖子,参数不起作用

时间:2011-05-11 01:17:34

标签: android http-post

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

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

我可以传递这些标题,但我似乎无法通过这些参数“用户名”,“密码”。这是我的代码:

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

我试图调试,但无法确定实体是否正确连接......我做错了什么?

先谢谢。 玛斯

4 个答案:

答案 0 :(得分: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);  

答案 1 :(得分:1)

我不太确定,根据您的描述,但似乎您的服务器需要一个JSON内容对象而不是URL中编码的数据。发送这样的内容作为帖子的正文

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

答案 2 :(得分: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发布时,它对我来说非常适合。

答案 3 :(得分:1)

这可能适合你。

假设您已经拥有了json对象。

注意:(1)在服务器中,您需要以utf-8(也在数据库中)处理请求。

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