无法在 Android Studio 中解析符号 HttpGet、HttpClient、HttpResponce

Cannot resolve symbol HttpGet,HttpClient,HttpResponce in Android Studio

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

我的 Activity 文件在这里:-

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

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

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

谢谢。

只需将其添加到您的 dependencies

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

仅供参考

Specify requirement for Apache HTTP Legacy library 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" />

您刚刚重建了您的项目

compile fileTree(dir: 'libs', include: ['*.jar'])

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

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

Google 正式建议开发者使用 Volley 库来处理网络相关的东西 Here, 所以是时候改用 Volley 了, 快乐编码

那只是在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'
}

对我来说,以下有帮助

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

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

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

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

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

useLibrary 'org.apache.http.legacy'

添加这个对我有用。

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

只需将这行代码添加到您的 build.gradle 文件中即可。

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

如果以上根本不起作用,请在下面的行中添加这些内容。

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'