1

In android studio I am trying to import HttpResponse and StringEntity, for this I am using import org.apache.http.HttpResponse and import org.apache.http.entity.StringEntity respectively. But android studio does not iddentifies these imports and is showing cannot resolve symbol. So how can I use these libraries in my project?

This is my code (.java file)

package com.example.abc.project.MongoHQ;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

import com.example.abc.project.Task;

public class SaveAsyncTask extends AsyncTask<Task, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Task... arg0) {
        try
        {
            Task task = arg0[0];

            QueryBuilder qb = new QueryBuilder();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(qb.buildContactsSaveURL());

            StringEntity params =new StringEntity(qb.createContact(task));
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode()<205)
            {
                return true;
            }
            else
            {
                return false;
            }
        } catch (Exception e) {
            //e.getCause();
            String val = e.getMessage();
            String val2 = val;
            return false;
        }
    }

}
Sajal Ali
  • 427
  • 1
  • 10
  • 21

2 Answers2

0

download these libraries: enter image description here

then paste them in your project folder >>app >lib >>here

Then go to AS,right click on project >>open module settings>>dependecies TAB>>Click +>>add file dependency >> locate the lib folder and import all three of them.And you are done!!

hemen
  • 1,460
  • 2
  • 16
  • 35
-1

First you have to check that in your libs folder

Make sure in Libs Folder Check that apache library

Then add into your gradle file like this 

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'

        defaultConfig {
            applicationId "info.tranetech.laundry"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    android {
        useLibrary 'org.apache.http.legacy'
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1
        compile 'com.android.support:design:23.0.1
        testCompile 'junit:junit:4.12'
        compile files('libs/android-async-http-1.4.4.jar')
        compile 'com.google.android.gms:play-services:8.4.0'
    }

This Is my gradle file

  [1]: https://i.stack.imgur.com/V6B4y.png
  [2]: https://i.stack.imgur.com/6VxrE.png

Also check external library

Arpit Patel
  • 7,212
  • 5
  • 56
  • 67