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 IOIOUtils
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
Findviewbyid Returns Null in a Dialog
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
Noclassdeffounderror in Eclipse
How to Write Ndef Records to Nfc Tag
Android Changing Floating Action Button Color
Failed to Run Sdkmanager --List with Java 9
How Can One Detect Airplane Mode on Android
How to Escape % in String.Format
Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This
Android M Permissions: Onrequestpermissionsresult() Not Being Called
Convert JSONarray to String Array
How to Downsample Images Within PDF File
Differences Between Exception and Error
How to Access an Iteration-Counter in Java's For-Each Loop
How to Activate Jmx on My Jvm for Access with Jconsole