0

我正在将数据从 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&email=b&password=c 或将参数放入帖子正文

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

4

2 回答 2

2

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

于 2013-06-07T04:38:39.450 回答
1

It's a little bit confusing that you mix URL parameters and post data in the same request. It's not unheard of but I would recommend that you do your login using another URL, for instance http://www.yourhost.com/signin and POST username and password.

You should also look into using a wrapper around your HTTP calls. Working with the DefaultHttpClient will have you writing a lot more code than if you used OkHttp, Volley or the excellent Android Asynchronous Http Client. Example using Android Async Http Client (with mixed URL and POST params):

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
    }
});
于 2013-06-07T05:39:39.030 回答