1

I used to do httppost like this:

protected String doInBackground(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://example.net/xxx/query.php?action=signin");
    String result = null;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", params[0]));
        nameValuePairs.add(new BasicNameValuePair("pass", params[1]));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        result = EntityUtils.toString(entity);
    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }
    return result;
}

but as you know httpclient is deprecated over api 22. So i found a class 'HttpRequest' which you can find in this link I need an alternative option to HttpClient in Android to send data to PHP as it is no longer supported I try this like this:

protected String doInBackground(String... params) {
    String result = null;

    try {
        HashMap<String, String> values = new HashMap<>();
        values.put("user", params[0]); 
        values.put("pass", params[1]);
        HttpRequest req = new HttpRequest("http://example.net/xxx/query.php?action=signin");
        result = req.preparePost().withData(values).sendAndReadString();
    } catch (Exception e) {
        result = e.getMessage();
    }

    return result;
}

but it gives an exception and exception message is the url("http://example.net/xxx/query.php?action=signin"). What am i doing wrong? Thank you.

Community
  • 1
  • 1
shanks
  • 342
  • 2
  • 4
  • 16

0 Answers0