尝试创建httpclient对象,但发生NoClassDefFoundError错误

时间:2019-07-26 23:03:07

标签: android youtube-api httpclient youtube-data-api android-youtube-api

我正在尝试从YouTube api获取数据,但是当我使用httpclient创建asyctask对象时,出现了NoClassDefFoundError错误。为什么会发生此错误?

    import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends YouTubeBaseActivity {

    private static final String TAG="MainActivity";

    private static String urll="https://www.googleapis.com/youtube/v3/" +
            "videos?part=snippet%2CcontentDetails%2Cstatistics&id=Ks-_Mh1QhMc&key="+YouTubeConfig.getApiKey()+" HTTP/1.1";

    YouTubePlayerView player;
    YouTubePlayer.OnInitializedListener playerInitializer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> a= new ArrayList<>();
        for (int x =0; x<100;x++ ){
            a.add(String.valueOf(x));
        }

        ListView lv= findViewById(R.id.izlenecekVideolar);
        ArrayAdapter<String> veriAdaptoru=new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
        lv.setAdapter(veriAdaptoru);
        player = (YouTubePlayerView) findViewById(R.id.youtubeView);
        int d = Log.d(TAG, "onCreate: ");


        new GetYoutube().execute();
        player.initialize(YouTubeConfig.getApiKey(),new YouTubePlayer.OnInitializedListener() {

                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                        List<String> izle=new ArrayList<>();
                        izle.add("W4hTJybfU7s");
                        izle.add("co1T0ue8zD8");
                        youTubePlayer.loadVideos(izle);
                        Log.d(TAG, "onInitializationSuccess: ");
                        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                        Log.d(TAG, "onInitializationFailure: ");
                    }
                }
        );

        Log.d(TAG, "onCreate:initialized ");
    }

    private static class GetYoutube extends AsyncTask<Void,String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... voids) {
            HttpClient hc=new DefaultHttpClient();
            HttpGet hget=new HttpGet(urll);
            try {
                HttpResponse response =hc.execute(hget);
                HttpEntity he= response.getEntity();
                String json= EntityUtils.toString(he);
                return json;


            }
            catch (Exception e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if (s != null)
            {
                try {
                    JSONObject jse=new JSONObject(s);
                    Log.e("response",jse.toString());
                }
                catch (Exception e){

                    e.printStackTrace();
                }

            }

        }
    }
}

LOGCAT

  

2019-07-27 01:49:22.162 21763-21763 / com.gamedu.yourchild E / Android运行时:致命异常:主要       流程:com.gamedu.yourchild,PID:21763       java.lang.NoClassDefFoundError:无法解决以下问题:Lorg / apache / commons / logging / LogFactory;           在org.apache.http.impl.client.AbstractHttpClient。(AbstractHttpClient.java:182)           在org.apache.http.impl.client.DefaultHttpClient。(DefaultHttpClient.java:150)           在com.gamedu.yourchild.MainActivity.onCreate(MainActivity.java:68)           在android.app.Activity.performCreate(Activity.java:7698)           在android.app.Activity.performCreate(Activity.java:7687)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1935)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7116)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)        由以下原因引起:java.lang.ClassNotFoundException:在路径:DexPathList上找不到类“ org.apache.commons.logging.LogFactory”。[[zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/base .apk”,zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_dependencies_apk.apk”、zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJ_g_ap_k_ap_k_ap_k_ap_kap_k_ap_k_aps_ap_k_ap_kap_k_ap_k_ap_k_ap_kap_k_ap_kap_k_ap_kap_k_ap_kap_k_ap_kap_kap_kap_k_ap_kap_kap_k_ap_kap_k_ap_kap_k_ap_kap_kap_kap_k_ap_kap_kap_k_ap_kap_k_aps_k_ap_k_aps_k_aps_k_ap是“ .apk”,zip文件“ /。 zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_slice_0_apk.apk”、zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==//“sp__ap_lib”数据/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_slice_2_apk.apk”、zip文件“ /data/app/com/game.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==//split_lib_slice_3”。 com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg == / split_lib_slice_4_apk.apk“,zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_slice_app.com.5k”你的d-Cf8th6YAffTJoMT1j3NJUg == / split_lib_slice_6_apk.apk“,zip文件” /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_slice_7_apk/app6/data6/apf.appk/kTy/app-y/kTy/app-y.3k/apk.apk“。 = / split_lib_slice_8_apk.apk“,zip文件“ /data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_slice_9_apk.apk”],nativeLibraryDirectories=[/data/app/J/NffU=6 / x86,/ system / lib,/ system / product / lib]]           在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)           在java.lang.ClassLoader.loadClass(ClassLoader.java:379)           在java.lang.ClassLoader.loadClass(ClassLoader.java:312)           在org.apache.http.impl.client.AbstractHttpClient。(AbstractHttpClient.java:182)           在org.apache.http.impl.client.DefaultHttpClient。(DefaultHttpClient.java:150)           在com.gamedu.yourchild.MainActivity.onCreate(MainActivity.java:68)           在android.app.Activity.performCreate(Activity.java:7698)           在android.app.Activity.performCreate(Activity.java:7687)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1935)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7116)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)           禁止:java.io.IOException:找不到用于dex位置/data/app/com.gamedu.yourchild-Cf8th6YAffTJoMT1j3NJUg==/split_lib_resources_apk.apk的原始dex文件           在dalvik.system.DexFile.openDexFileNative(本机方法)           在dalvik.system.DexFile.openDexFile(DexFile.java:365)           在dalvik.system.DexFile。(DexFile.java:107)           在dalvik.system.DexFile。(DexFile.java:80)           在dalvik.system.DexPathList.loadDexFile(DexPathList.java:444)           在dalvik.system.DexPathList.makeDexElements(DexPathList.java:403)           在dalvik.system.DexPathList。(DexPathList.java:164)           在dalvik.system.BaseDexClassLoader。(BaseDexClassLoader.java:126)           在dalvik.system.BaseDexClassLoader。(BaseDexClassLoader.java:101)           在dalvik.system.PathClassLoader。(PathClassLoader.java:74)   2019-07-27 01:49:22.163 21763-21763 / com.gamedu.yourchild E / AndroidRuntime:在com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:87)           在com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:116)           在android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:114)           在android.app.ApplicationLoaders.getClassLoaderWithSharedLibraries(ApplicationLoaders.java:60)           在android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:861)           在android.app.LoadedApk.getClassLoader(LoadedApk.java:958)           在android.app.LoadedApk.getResources(LoadedApk.java:1196)           在android.app.ContextImpl.createAppContext(ContextImpl.java:2462)           在android.app.ContextImpl.createAppContext(ContextImpl.java:2454)           在android.app.ActivityThread.handleBindApplication(ActivityThread.java:6105)           在android.app.ActivityThread.access $ 1100(ActivityThread.java:211)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1778)

1 个答案:

答案 0 :(得分:0)

首先,这类错误通常不是编码错误,而是因为基本APK中的某些损坏文件可以从logcat中清楚地看到。请参阅 ClassLoader 无法从基本apk文件中加载特定的类,因此发生致命异常 ClassNotFound

解决方案: 基本上,这不是一个确定的解决方案,但值得一试。我遇到了类似的问题,在搜索了很多之后,我发现的唯一合理的解决方案是删除所有数据,然后从设备上卸载该应用程序,然后重新安装它。不过,这不是一个确定的解决方案。