带有AsyncHttpClient的java.lang.NoClassDefFoundError - API 19

时间:2016-04-11 15:31:31

标签: java android android-async-http

有很多关于此问题的主题,但没有一个有效:/

我正在运行android studio,我在gradle中添加了这个库,然后 gradle clean 构建项目运行应用

不幸的是,每次运行应用程序都会失败

Process: com.example.gta.fab1, PID: 5775
            java.lang.NoClassDefFoundError: com.loopj.android.http.AsyncHttpClient
                at com.example.gta.fab1.TabFragments.WhoFragment.uploadToServer(WhoFragment.java:129)
                at com.example.gta.fab1.TabFragments.WhoFragment.access$100(WhoFragment.java:41)
                at com.example.gta.fab1.TabFragments.WhoFragment$1.onClick(WhoFragment.java:85)
                at com.facebook.FacebookButtonBase.callExternalOnClickListener(FacebookButtonBase.java:216)
                at com.facebook.share.widget.ShareButtonBase.access$000(ShareButtonBase.java:39)
                at com.facebook.share.widget.ShareButtonBase$1.onClick(ShareButtonBase.java:167)
                at com.facebook.FacebookButtonBase$1.onClick(FacebookButtonBase.java:385)
                at android.view.View.performClick(View.java:4438)
                at android.view.View$PerformClick.run(View.java:18422)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5001)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                at dalvik.system.NativeStart.main(Native Method)

我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.gta.fab1"
        multiDexEnabled true
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.loopj.android:android-async-http:1.4.9'
}

我的课程:

package com.example.gta.fab1.TabFragments;


import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.loopj.android.http.*;

import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;
import cz.msebera.android.httpclient.Header;


public class WhoFragment extends Fragment {

    CallbackManager callbackManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(
                R.layout.who_fragment, container, false);

                JSONObject post_json = jSonBuilder(facebook_id, activity);
                try{
                    uploadToServer(post_json);
                    Log.d("output", post_json.toString(2));
                } catch(java.io.IOException | org.json.JSONException e){
                    throw new RuntimeException(e);
                }

        return mRelativeLayout;
    }

    private JSONObject jSonBuilder(String fb_id,FragmentMain activity ){
        JSONObject parent = new JSONObject();

        try{
            parent.put("user_fb_id",fb_id);
            parent.put("locations", activity.serialize.getArrayOfRestaurants());
            parent.put("time_propositions", activity.serialize.getArrayOfTimeSchedule());
            return parent;
        }catch(org.json.JSONException e){
            throw new RuntimeException(e);
        }
    }

    private void uploadToServer(JSONObject post_json) throws IOException, org.json.JSONException {

        AsyncHttpClient client = new AsyncHttpClient();

        String url = "http://myurl/api/postjson/1";
        StringEntity se = null;
        try {
            se = new StringEntity(post_json.toString());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        client.post(null, url, se, "application/json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
                System.out.print("success");
            }

            @Override
            public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
                System.out.print("fail");
            }
        });
    }
}

有些回答是关于手动添加jar lib并在gradle.build中引用它,但是在插件的网站上也没有工作,文档很清楚:

https://guides.codepath.com/android/Using-Android-Async-Http-Client

我在这里没有看到我的错误:/

提前多多感谢

编辑:将jar文件导入libs文件夹,然后添加到库,并在gradle中手动导入头文件的libs:

compile 'cz.msebera.android:httpclient:4.3.+'

给出不同的错误:

java.lang.NoClassDefFoundError: com.loopj.android.http.LogHandler

必须有一个错误:(我没有看到任何其他选项

编辑2: 在API 21上运行良好......,这个库与API 19不兼容吗?

2 个答案:

答案 0 :(得分:2)

  1. 启用multidex

    defaultConfig {
    ...
     // Enabling multidex support.
    multiDexEnabled true
    }
    
  2. 添加依赖

    dependencies {
        compile 'com.android.support:multidex:1.0.1'
    }
    
  3. 在您的应用中添加代码

    @Override
    protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
    }
    
  4. 这对我有用,我希望它对你有所帮助。

答案 1 :(得分:0)

它可能与multiDex功能有关。

您可以通过删除" multiDexEnabled true"来禁用它。你的gradle文件中的行。

或关注the multidex guide

  1. 设置multiDexEnabled true
  2. 添加依赖项' com.android.support:multidex:1.0.0'
  3. 将应用程序替换为MultiDexApplication