【问题标题】:android httpclient send post using the url as parameters?android httpclient 使用 url 作为参数发送帖子?
【发布时间】:2013-06-03 07:11:47
【问题描述】:

我正在将数据从 android 发送到 web,它使用 httpclient 使用这样的代码

DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://host");
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("method", "signIn"));
    nvps.add(new BasicNameValuePair("email",u));
    nvps.add(new BasicNameValuePair("password",pass));
    UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
    httppost.setEntity(p_entity);

我有点困惑,如果这段代码将参数作为参数放入 url,比如url?method=a&amp;email=b&amp;password=c 或将参数放入帖子正文中

我应该做的是构造一个 http 帖子到这个 url url?method=a 并在帖子正文中使用电子邮件和密码参数

【问题讨论】:

标签: android httpclient


【解决方案1】:

在同一个请求中混合 URL 参数和发布数据有点令人困惑。这并非闻所未闻,但我建议您使用另一个 URL 登录,例如 http://www.yourhost.com/signin 和 POST 用户名和密码。

您还应该考虑在 HTTP 调用周围使用包装器。与使用 OkHttp、Volley 或出色的 Android Asynchronous Http Client 相比,使用 DefaultHttpClient 将使您编写更多的代码。使用 Android 异步 Http 客户端的示例(使用混合 URL 和 POST 参数):

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("email", "u");
params.put("password", "pass");
client.post("http://host?method=signIn", params, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        // handle response here
    }
});

【讨论】:

    【解决方案2】:

    您应该阅读HttpMethods。根据定义,HttpPost 在其主体上传递参数,而不是在查询字符串中。另一方面,HttpGet 应该在查询字符串中传递参数。 另外,这里的entity代表body。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多