55

I just copy all the jar files of Http but Android Studio cann't import all these jar files.It gives an error : Cannot resolve symbol HttpGet,HttpClient,HttpResponse.

My Activity file is here:-

public class MainActivity extends AppCompatActivity {

ArrayList<Product>  productslist;
ProductAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    productslist = new ArrayList<Product>();
    new JSONAsyncTask().execute("http://opencart.codeniques.com/myshop/?route=feed/web_api/products&id=60&key=test123");

    ListView listView = (ListView)findViewById(R.id.list);
    adapter = new ProductAdapter(getApplicationContext(),R.layout.row,productslist);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),productslist.get(position).getName(),Toast.LENGTH_LONG).show();
        }
    });
}

class JSONAsyncTask extends AsyncTask<String,Void,Boolean>{

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try{
            HttpGet httppost = new HttpGet(params[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            int status = response.getStatusLine().getStatusCode();

            if(status == 200){
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("products");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Product actor = new Product();

                    actor.setId(object.getString("id"));
                    actor.setName(object.getString("name"));
                    actor.setDescription(object.getString("description"));
                    actor.setHref(object.getString("href"));
                    actor.setPrice(object.getString("pirce"));
                    actor.setImage(object.getString("thumb"));
                    actor.setSpecial(object.getString("special"));
                    actor.setRating(object.getString("rating"));

                    productslist.add(actor);
            }
                return  true;
            }
        }catch (JSONException e){
            Log.e("Error :",e.getMessage());
        }catch (ParseException e){
            Log.e("Error :",e.getMessage());
        }catch (IOException e){
            Log.e("Error :",e.getMessage());
        }catch (Exception e){
            Log.e("Error :",e.getMessage());
        }
        return  false;
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(aBoolean == false){
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        }
    }
}}

and here my gradle is here:-

apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "android.catalyst.com.newjsonarray"
    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'])

testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'}
Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26
Anand Jain
  • 2,365
  • 7
  • 40
  • 66

12 Answers12

105

Just add this in your dependencies

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

Finally

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])    
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
}

And also add this code:

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

For more information, see Specify requirement for Apache HTTP Legacy library in the docs:

If your app is targeting API level 28 (Android 9.0) or above, you must include the following declaration within the element of AndroidManifest.xml.

 <uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 2
    Partially worked. I tried the same, a few classes got accessible by this - HttpEntity, BufferedHttpEntity as well as HttpResponse. But, still unable to access HttpClient, HttpGet and DefaultHttpClient – Narendra Singh Apr 28 '16 at 09:11
  • @MrNarendra how did you solve this for other classes? – Shruti Nov 20 '17 at 05:32
  • 1
    @IntelliJAmiya used `org.apache.http.legacy` library but HttpGet, HttpClient and DefaultHttpClient are deprecated. – Shruti Nov 20 '17 at 05:44
  • 1
    @Shruti Ji, Yes. Kindly check https://stackoverflow.com/questions/15336477/deprecated-java-httpclient-how-hard-can-it-be – IntelliJ Amiya Nov 20 '17 at 05:46
  • 1
    Additionally, if you are targeting SDK 28+, then [read this](https://stackoverflow.com/a/50779232/7551190) too – Kathir Nov 28 '18 at 13:42
  • Also,I had to add -> exclude 'META-INF/DEPENDENCIES' <- to build.gradle – Ehsan Jul 02 '20 at 11:43
38

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:

In dependencies add:

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

and also add this

android {
    useLibrary 'org.apache.http.legacy'
}
Sanju
  • 663
  • 9
  • 20
  • 12
    `useLibrary 'org.apache.http.legacy'` should be enough to solve this issue, the other libs were not needed (at least in my case). – Franco Feb 13 '16 at 07:02
14

HttpClient was deprecated in API Level 22 and removed in API Level 23. You have to use URLConnection.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
TheSecond
  • 183
  • 1
  • 1
  • 8
9

please add these codes to your dependencies. It will work.

implementation 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:23.1.0'
    implementation 'com.android.support:design:23.1.0'
    implementation 'com.android.support:cardview-v7:23.1.0'
    implementation 'com.android.support:recyclerview-v7:23.1.0'

    implementation 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
Kathir
  • 1,282
  • 1
  • 15
  • 19
Rahul K A
  • 322
  • 4
  • 4
4

Please remove all jar files of Http from libs folder and add below dependencies in gradle file :

compile 'org.apache.httpcomponents:httpclient:4.5'
compile 'org.apache.httpcomponents:httpcore:4.4.3'

Thanks.

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
1

Google officially suggests to developers to use Volley library for networking related stuff Here, so Its time to switch to Volley, Happy coding

Avinash Sharma
  • 60
  • 1
  • 13
1

For me, the below helped

Find org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional, add it to your dependency.

Source

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
1

Please remove all jar files of Http from 'libs' folder and add below dependencies in gradle file:

compile 'org.apache.httpcomponents:httpclient:4.5'
compile 'org.apache.httpcomponents:httpcore:4.4.3'

or

useLibrary 'org.apache.http.legacy'
Bojan B
  • 2,091
  • 4
  • 18
  • 26
1

Just add this line of code in your build.gradle file and it will work.

implementation 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

Also add these below line if above did not work at all.

compile 'com.google.android.gms:play-services:+'
compile ('org.apache.httpcomponents:httpmime:4.2.6'){
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient:4.2.6'

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
0

You just rebuilt your project

compile fileTree(dir: 'libs', include: ['*.jar'])
om_jaipur
  • 2,176
  • 4
  • 30
  • 54
0

That is just in SDK 23, Httpclient and others are deprecated. You can correct it by changing the target SDK version like below:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "vahid.hoseini.com.testclient"
        minSdkVersion 10
        targetSdkVersion 22
        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:22.1.1'
}
VahidHoseini
  • 495
  • 2
  • 10
  • 28
0

adding this worked for me.

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Gomez NL
  • 912
  • 9
  • 12