2

I am trying to make some http request with parameters. Currently I am using

 private List<NameValuePair> mParams = new ArrayList<NameValuePair>();
 private DefaultHttpClient mHttpClient;
 private HttpPost mHttpPost;

these for making http request and it is working fine. But the problem is android studio is showing deprecated warning for all these 3 . I tried by using HttpClientBuilder.create().build(); but android studio cannot import the libraries for HttpClientBuilder I tried downloading and adding this jar dependency but still doesn't work. This is my code

        mHttpClient = new DefaultHttpClient();
        mHttpPost = new HttpPost(url);
        mParams.add(new BasicNameValuePair("key", value));
        mHttpPost.setEntity(new UrlEncodedFormEntity(mParams));

All these lines are showing deprecated warning, which is the alternative method for this?

dev
  • 1,085
  • 4
  • 19
  • 26
  • Check: http://stackoverflow.com/questions/24049197/sending-an-http-post-request – Paresh Mayani Aug 04 '15 at 05:26
  • HttpClient is deprecated use HttpUrlConnection. Check this one : http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post – SANAT Aug 04 '15 at 05:35
  • possible duplicate of [Android deprecated apache module (HttpClient, HttpResponse, etc.)](http://stackoverflow.com/questions/29294479/android-deprecated-apache-module-httpclient-httpresponse-etc) – Sufian Aug 04 '15 at 05:35

5 Answers5

5

You can use HttpsURLConnection instead of DefaultHttpClient. Also NameValuePair is deprecated, use Uri.Builder and appendQueryParameter to send request parameters.

Try following piece of code:

URL url = new URL("http://myurl.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
       .appendQueryParameter("key1", valu1)
       .appendQueryParameter("key2", value2);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
buczek
  • 2,011
  • 7
  • 29
  • 40
Sarath Kn
  • 2,680
  • 19
  • 24
2

DefaultHttpClient class is deprecated instead you can use HttpUrlConnection class . HttpUrlConnection hassimple API . NameValuePair class may be deprecated in api 22. You can Use ContentValues (android.content.ContentValues) instead of NameValuePair.

ContentValues contentValues = new ContentValues();
        contentValues.put("Param_name1", Param_value1);
        contentValues.put("Param_name2", Param_value2);

I am using Okhttp client to make get and post call. http://square.github.io/okhttp/

I think this link will help you http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html

Pruthviraj
  • 560
  • 6
  • 23
1

from this link you can download the latest jar for httpclient4.5. and use

HttpClient httpClient = HttpClientBuilder.create().build();
RamBabu Pudari
  • 912
  • 7
  • 19
1

HttpClient is deprecated since API level 22. Prefer HttpURLConnection for new code.

Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling. Apache HTTP client has fewer bugs in Android 2.2 (Froyo) and earlier releases. For Android 2.3 (Gingerbread) and later, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. See the Android Developers Blog for a comparison of the two HTTP clients.

zheek
  • 742
  • 1
  • 11
  • 22
Omji Mehrotra
  • 254
  • 1
  • 7
1

IDE prompts warning on new DefaultHttpClient, mark this class as deprecated [deprecated in Api 22].

To solve it, use HttpClientBuilder :

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("http://www.google.com");
    HttpResponse response = client.execute(request);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198