I Need an Alternative Option to Httpclient in Android to Send Data to PHP as It Is No Longer Supported

I need an alternative option to HttpClient in Android to send data to PHP as it is no longer supported

The HttpClient was deprecated and now removed:

org.apache.http.client.HttpClient:

This interface was deprecated in API level 22.
Please use openConnection() instead. Please visit this webpage for further details.

means that you should switch to java.net.URL.openConnection().

See also the new HttpURLConnection documentation.

Here's how you could do it:

URL url = new URL("http://some-server");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);

IOUtils documentation: Apache Commons IO

IOUtils Maven dependency: http://search.maven.org/#artifactdetails|org.apache.commons|commons-io|1.3.2|jar

HTTPClient Alternative?

I use the Google Volley SDK. It is way faster than using the standard Android HTTP client.

HttpClient deprecated / Connect with PHP server - Android API 22

first: It's already been answered
Sending Http request for Android 22+

second: I've made a class that meets your needs, which allow you to send request and receive response with one line of code (It's also explained in post above)

Here is the link for the my class:

HttpRequest

HttpClient won't import in Android Studio

HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22 (compile 'com.android.support:appcompat-v7:22.2.0')

If you need sdk 23, add this to your gradle:

android {
useLibrary 'org.apache.http.legacy'
}

You also may try to download and include HttpClient jar directly into your project or use OkHttp instead



Related Topics



Leave a reply



Submit