无法在Android Studio中解析符号HttpGet,HttpClient,HttpResponse

我只复制了gradle的所有jar文件,但是Android Studio无法导入所有这些jar文件。它给出了一个错误:Cannot resolve symbol HttpGet,HttpClient,HttpResponse

我的gradle文件在这里:-

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();
        }
    }
}}

我的gradle在这里:

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'}
11个解决方案
97 votes

只需将其添加到您的依赖项中

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

最后

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'
}

并添加以下代码:

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

费耶

指定对Apache HTTP旧版库的要求(如果您的应用是   定位API级别28(Android 9.0)或更高版本,则必须包含   以下元素中的声明   AndroidManifest.xml。

 <uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
IntelliJ Amiya answered 2020-01-14T14:12:29Z
36 votes

sdk 23中不再支持HttpClient。您必须使用URLConnection或降级到sdk 22(编译“ com.android.support:appcompat-v7:22.2.0”)

如果需要sdk 23,请将其添加到gradle:

在依赖项中添加:

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

并添加这个

android {
    useLibrary 'org.apache.http.legacy'
}
Sanju answered 2020-01-14T14:13:02Z
13 votes

HttpClient在API级别22中已弃用,在API级别23中已删除。您必须使用URLConnection

TheSecond answered 2020-01-14T14:13:22Z
6 votes

请将这些代码添加到您的依赖项中。 它会工作。

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'
}
Rahul K A answered 2020-01-14T14:13:46Z
4 votes

请从libs文件夹中删除所有Http的jar文件,并在gradle文件中添加以下依赖项:

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

谢谢。

AndiGeeky answered 2020-01-14T14:14:11Z
1 votes

Google正式建议开发人员使用Volley库来进行与网络相关的工作。所以该切换到Volley了,快乐编码

Avinash Sharma answered 2020-01-14T14:14:31Z
1 votes

对我来说,以下内容对您有帮助

Android/Sdk/platforms/android-23/optional,中找到org.apache.http.legacy.jar,将其添加到您的依赖项中。

资源

Atul O Holic answered 2020-01-14T14:15:00Z
1 votes

请从'libs'文件夹中删除Http的所有jar文件,并在gradle文件中添加以下依赖项:

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

要么

useLibrary 'org.apache.http.legacy'
Qaisar Ayub answered 2020-01-14T14:15:25Z
0 votes

您刚刚重建了项目

compile fileTree(dir: 'libs', include: ['*.jar'])
webomjaipur answered 2020-01-14T14:15:45Z
0 votes

仅在SDK 23中,不建议使用Httpclient和其他客户端。 您可以通过更改目标SDK版本来更正它,如下所示:

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'
}
hoseini_pro answered 2020-01-14T14:16:05Z
0 votes

添加这个对我有用。

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Gomez NL answered 2020-01-14T14:16:26Z
translate from https://stackoverflow.com:/questions/32607257/cannot-resolve-symbol-httpget-httpclient-httpresponce-in-android-studio