0

I am sending data from android to the web, which is using httpclient using the code like this

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

I am a little confused, if this code is putting the parameters into the url as argument, like url?method=a&email=b&password=c or putting the parameters into the post body

What I should do is to construct a http post to this url url?method=a with email and password parameters in post body

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
user824624
  • 7,077
  • 27
  • 106
  • 183

2 Answers2

2

You should read about HttpMethods.By definition, HttpPost pass the parameters on its body, and not in the query string. HttpGet in the other hand, should pass the parameters in the query string. Besides, the entity here stands for the body.

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
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
    }
});
britzl
  • 10,132
  • 7
  • 41
  • 38