【问题标题】:Android: Http post with parameters not workingAndroid:带有参数的 Http 帖子不起作用
【发布时间】:2011-05-11 01:17:34
【问题描述】:

我需要创建一个带有参数的 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);  

我尝试调试,但无法查看实体是否正确附加...我做错了什么?

提前致谢。 马兹

【问题讨论】:

    标签: android http-post


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 正是我需要的!非常感谢。 :)
    • 完美——补充一下,我喜欢使用定义的常量:HTTP.UTF_8
    • 为什么这行得通而 OP 的代码却不行?我遇到了类似的问题,但只有十分之一的服务器。
    【解决方案2】:

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

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

    【讨论】:

    • 感谢 Dmon,Femis 代码运行良好。但即使是你的建议也会让我找到正确的代码:)
    【解决方案3】:
    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 发布时非常适合我。

    【讨论】:

    • 感谢 Sumant,我曾在某个阶段尝试过,但没有奏效。 :(
    【解决方案4】:

    这可能对你有用。

    假设您已经拥有 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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多