Ryan Ryan - 2 years ago 124
Android Question

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

Currently I'm using

to send data to my
PHP server
from an
Android app
but all those methods were deprecated in API 22 and removed in API 23, so what are the alternative options to it?

I searched everywhere but I didn't find anything.

Answer Source

The HttpClient documentation points you in the right direction:


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().

Here's how you could do it:

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

// 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");

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download