8

I'm using android studio to create an app that makes a GET request to a server. My code is this:

import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGetHC4;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class HTTPHelper
{
    private String base;

    public HTTPHelper (String base)
    {
        this.base = base;
    }

    public String doGet (String path)
    {
        try
        {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGetHC4 get = new HttpGetHC4(path);
            CloseableHttpResponse response = client.execute(get);

            return "";
        }
        catch (Exception e)
        {
            return null;
        }
    }
}

The problem is that Android Studio marks the line

client.execute(get);

with an error:

enter image description here

saying "Cannot access org.apache.http.client.HttpClient"

Here's my gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "principal.halloween"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.pubnub:pubnub-android:3.7.5'
    compile 'com.google.code.gson:gson:2.3.1'
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
    compile 'org.apache.httpcomponents:httpclient:4.3.6'

}
1
  • have u try using a requestBuilder? its a slightly diff setup than u have. Maybe something wrong with the client builder is NOT affect same way in the case of RequestBuilder.build followed by 'execute' baeldung.com/httpclient-custom-http-header re: section 4 Commented Oct 6, 2015 at 3:58

2 Answers 2

6

In Android SDK 23

HttpClient is deprecated because it inference, you can migrate your code in HttpURLConnection

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

You can use this, but it's not recommended anymore

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

For the HttpURLConnection

URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
Sign up to request clarification or add additional context in comments.

Comments

0

This is because HttpClient is not supported in sdk 23 onwards now. Try adding this in your gradle.

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

if that doesn't works, just download HttpClient jar file and add to your library.

If still it is not working, you have to downgrade your compileSdkVersion to 22 or lower.

1 Comment

first of all, you dont download HttpClient cause that wont work either, you need to download httpclient-android. compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile('org.apache.httpcomponents:httpmime:4.3.6') { exclude module: 'httpclient' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.