Makame Sharif
Makame Sharif

Reputation: 65

httpclient, DefaultHttpClient, HttpPost ,BasicNameValuePair,NameValuePair are deprecated

I'm new in android app. The main point i want to send data from android app to php page but i get the following errors in my codes

org.apache.http.impl.client.defaulthttpclient is deprecated.

I had try a lot of way but not yet successed.

my android is 1.2.1.1 (API 22: 5.1.1).

below is my code.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("dataOne", "Hello"));
    nameValuePairs.add(new BasicNameValuePair("dataTwo", "Android World"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

Upvotes: 1

Views: 1064

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

You can:

  • Switch to using Apache's HttpClient standalone library, or

  • Switch to using Java's standard HttpUrlConnection, or

  • Switch to an independent HTTP access library, like OkHttp, or

  • Continue using the deprecated classes and methods, bearing in mind that doing so will become increasingly difficult with time

Upvotes: 2

Related Questions