0

I want to send data from my android app to servlet. i was trying to use this in my app :

    private boolean postData(String s) {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(s); // s is my url

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", "1003"));
        nameValuePairs.add(new BasicNameValuePair("name", "Anil"));
        nameValuePairs.add(new BasicNameValuePair("loc", "Kalyan"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        return false;
    }
    return true;
}

I was this method but was not able to import these files :

    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;

How to impoert these files in android studio?? Or else is there any other method to do so?? Thanx in advance

Rahul Verma
  • 3
  • 1
  • 5

1 Answers1

0

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead

 URL url = new URL(path);
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.setRequestMethod("POST");
 conn.setConnectTimeout(10000);
 conn.setReadTimeout(2000);
 conn.setDoOutput(true);
 byte[] bypes = params.toString().getBytes();
 conn.getOutputStream().write(bypes);
 InputStream inStream = conn.getInputStream();
lynn518
  • 26
  • 3