Ryan Ryan - 1 year ago 102
Android Question

I need an option to HttpClient in Android to send data to PHP as it is deprecated in API 22

Currently I'm using

to send data to my
PHP server
from an
Android app
but those all methods are deprecated in API 22 so what is the option 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