I have a simple class written in Android Studio:
package com.mysite.myapp;
import org.apache.http.client.HttpClient;
public class Whatever {
public voi
To use Apache HTTP for SDK Level 23:
Top level build.gradle - /build.gradle
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// Lowest version for useLibrary is 1.3.0
// Android Studio will notify you about the latest stable version
// See all versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
}
...
}
Notification from Android studio about gradle update:
Module specific build.gradle - /app/build.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
...
useLibrary 'org.apache.http.legacy'
...
}
As mentioned before, org.apache.http.client.HttpClient
is not supported any more in:
SDK (API level) #23.
You have to use java.net.HttpURLConnection
.
If you want to make your code (and life) easier when using HttpURLConnection
, here is a Wrapper
of this class that will let you do simple operations with GET
, POST
and PUT
using JSON
, like for example, doing a HTTP PUT
.
HttpRequest request = new HttpRequest(API_URL + PATH).addHeader("Content-Type", "application/json");
int httpCode = request.put(new JSONObject().toString());
if (HttpURLConnection.HTTP_OK == httpCode) {
response = request.getJSONObjectResponse();
} else {
// log error
}
httpRequest.close()
Feel free to use it.
package com.calculistik.repository;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
* <p>
* Copyright © 2017, Calculistik . All rights reserved.
* <p>
* Oracle and Java are registered trademarks of Oracle and/or its
* affiliates. Other names may be trademarks of their respective owners.
* <p>
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* https://netbeans.org/cddl-gplv2.html or
* nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific
* language governing permissions and limitations under the License.
* When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this particular file
* as subject to the "Classpath" exception as provided by Oracle in the
* GPL Version 2 section of the License file that accompanied this code. If
* applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* <p>
* Contributor(s):
* Created by alejandro tkachuk @aletkachuk
* www.calculistik.com
*/
public class HttpRequest {
public static enum Method {
POST, PUT, DELETE, GET;
}
private URL url;
private HttpURLConnection connection;
private OutputStream outputStream;
private HashMap<String, String> params = new HashMap<String, String>();
public HttpRequest(String url) throws IOException {
this.url = new URL(url);
connection = (HttpURLConnection) this.url.openConnection();
}
public int get() throws IOException {
return this.send();
}
public int post(String data) throws IOException {
connection.setDoInput(true);
connection.setRequestMethod(Method.POST.toString());
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
this.sendData(data);
return this.send();
}
public int post() throws IOException {
connection.setDoInput(true);
connection.setRequestMethod(Method.POST.toString());
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
return this.send();
}
public int put(String data) throws IOException {
connection.setDoInput(true);
connection.setRequestMethod(Method.PUT.toString());
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
this.sendData(data);
return this.send();
}
public int put() throws IOException {
connection.setDoInput(true);
connection.setRequestMethod(Method.PUT.toString());
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
return this.send();
}
public HttpRequest addHeader(String key, String value) {
connection.setRequestProperty(key, value);
return this;
}
public HttpRequest addParameter(String key, String value) {
this.params.put(key, value);
return this;
}
public JSONObject getJSONObjectResponse() throws JSONException, IOException {
return new JSONObject(getStringResponse());
}
public JSONArray getJSONArrayResponse() throws JSONException, IOException {
return new JSONArray(getStringResponse());
}
public String getStringResponse() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
for (String line; (line = br.readLine()) != null; ) response.append(line + "\n");
return response.toString();
}
public byte[] getBytesResponse() throws IOException {
byte[] buffer = new byte[8192];
InputStream is = connection.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int bytesRead; (bytesRead = is.read(buffer)) >= 0; )
output.write(buffer, 0, bytesRead);
return output.toByteArray();
}
public void close() {
if (null != connection)
connection.disconnect();
}
private int send() throws IOException {
int httpStatusCode = HttpURLConnection.HTTP_BAD_REQUEST;
if (!this.params.isEmpty()) {
this.sendData();
}
httpStatusCode = connection.getResponseCode();
return httpStatusCode;
}
private void sendData() throws IOException {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append((result.length() > 0 ? "&" : "") + entry.getKey() + "=" + entry.getValue());//appends: key=value (for first param) OR &key=value(second and more)
}
sendData(result.toString());
}
private HttpRequest sendData(String query) throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(query);
writer.close();
return this;
}
}
Add these two lines under dependencies
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
then
useLibrary 'org.apache.http.legacy'
under the android
Android 6.0 (API Level 23) release removes support for the Apache HTTP client. Hence you can not use this library directly in API 23. But there is a way to use it. Add useLibrary ‘org.apache.http.legacy’ in your build.gradle file as below-
android {
useLibrary 'org.apache.http.legacy'
}
If this does not work you may apply the following hack-
– Copy org.apache.http.legacy.jar which is in /platforms/android-23/optional path of your Android SDK directory to your project’s app/libs folder.
– Now Add compile files(‘libs/org.apache.http.legacy.jar’) inside dependencies{} section of build.gradle file.
Simply use this :-
android {
.
.
.
useLibrary 'org.apache.http.legacy'
.
.
.
}