安卓:带参数的HTTP POST请求不起作用

10

我需要创建一个HTTP POST请求并带上参数。我知道有很多示例,我已经尝试使用HTTPparams,NameValuePair等,但是似乎无法得到正确的格式以便于服务器进行处理。

服务器类型:使用JSON进行数据传输的REST API
内容类型:application/json
接受类型:application/json
内容长度:47
{"username":"abcd","password":"1234"}

我可以传递这些标头,但是我似乎无法传递这些参数“username”和“password”。这里是我的代码:

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

我试图进行调试,但无法看到实体是否已正确附加...我做错了什么?

先感谢您。 Maaz

4个回答

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

3
完美 - 另外,我喜欢使用定义的常量:HTTP.UTF_8 - Richard Le Mesurier
为什么这个代码能够正常工作,而原始问题的代码却不能?我遇到了类似的问题,但只在10台服务器中的1台上出现。 - Gaurav Bhor

1

根据您的描述,我不太确定,但似乎您的服务器期望一个JSON内容对象,而不是数据被编码在URL中。请将以下内容作为您的POST请求的主体发送:

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

1
谢谢Dmon,Femis的代码很好用。但是即使是你的建议也会让我找到正确的代码 :) - Maaz

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

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 POST时,它对我非常完美地起作用。


谢谢Sumant,我曾经尝试过这个,但是没有成功。:( - Maaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接