问题描述
我想在 Android Studio (v. 1.1.0) 中编写应用程序来浏览 reddit.我找到了 API 包装器 jReddit 并想使用它.
I would like to write application in Android Studio (v. 1.1.0) to browse reddit. I found API wrapper jReddit and would like to use it.
起初我只是尝试初始化RestClient(当我点击按钮时调用方法),但我得到错误:
At first I just try to initialize RestClient (method is called when I click on button), but I get error:
528-1528/com.example.sk1x1.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sk1x1.myapplication, PID: 1528
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/util/Args;
at org.apache.http.conn.util.PublicSuffixMatcherLoader.load(PublicSuffixMatcherLoader.java:58)
at org.apache.http.conn.util.PublicSuffixMatcherLoader.getDefault(PublicSuffixMatcherLoader.java:87)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:884)
at com.github.jreddit.utils.restclient.HttpRestClient.<init>(HttpRestClient.java:66)
at com.example.sk1x1.myapplication.MainActivity.tlacitko(MainActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.util.Args" on path: DexPathList[[zip file "/data/app/com.example.sk1x1.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.apache.http.conn.util.PublicSuffixMatcherLoader.load(PublicSuffixMatcherLoader.java:58)
at org.apache.http.conn.util.PublicSuffixMatcherLoader.getDefault(PublicSuffixMatcherLoader.java:87)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:884)
at com.github.jreddit.utils.restclient.HttpRestClient.<init>(HttpRestClient.java:66)
at com.example.sk1x1.myapplication.MainActivity.tlacitko(MainActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: org.apache.http.util.Args
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 19 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
初始化代码:
public void tlacitko(View v)
{
RestClient restClient;
restClient = new PoliteHttpRestClient();//= new HttpRestClient();
//User user = new User(restClient, "nick", "dasdasd");
}
要编译的库:
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/jreddit-1.0.2.jar')
compile files('libs/httpclient-4.4.jar')
compile files('libs/jna-4.1.0.jar')
compile files('libs/jna-platform-4.1.0.jar')
compile files('libs/commons-io-2.4.jar')
我想我有所有需要的库,我在哪里做错了?
I think I have all needed libs, where can I do mistake?
更新
我只是再试一次我的项目.我添加依赖项:
I just try again my project. I add dependency:
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile('com.github.jreddit:jreddit:1.0.3') {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude module: 'junit'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}
但是当我尝试运行项目时,出现错误:
but when I try to run project, I get error:
Error:duplicate files during packaging of APK D:\ZCU\AndroidStudioProjects\JredditTest\app\build\outputs\apk\app-debug-unaligned.apk
Path in archive: META-INF/DEPENDENCIES
Origin 1: C:\Users\Sk1X1\.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpmime\4.3.5\1dd0d38df9c6d21e893f2e52403f1cd99e91cd81\httpmime-4.3.5.jar
Origin 2: C:\Users\Sk1X1\.gradle\caches\modules-2\files-2.1\org.apache.maven\maven-repository-metadata\3.0\e3c41f7565b1e189ff7a312796b9d2c470c09a8b\maven-repository-metadata-3.0.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
}
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/DEPENDENCIES
File 1: C:\Users\Sk1X1\.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpmime\4.3.5\1dd0d38df9c6d21e893f2e52403f1cd99e91cd81\httpmime-4.3.5.jar
File 2: C:\Users\Sk1X1\.gradle\caches\modules-2\files-2.1\org.apache.maven\maven-repository-metadata\3.0\e3c41f7565b1e189ff7a312796b9d2c470c09a8b\maven-repository-metadata-3.0.jar
此错误仅发生在 Android Studio 中.我在 Eclipse 中尝试了 Maven 依赖,jreddit 在那里工作得很好.
This error occurs only in Android studio. I tried Maven dependency in Eclipse and jreddit work great there.
推荐答案
我是 jReddit 项目的当前维护者.
I am the current maintainer for the jReddit project.
我最近创建了一个使用 jreddit 库的示例 android 示例.没有为这个例子做太多事情,但是 android 应用程序可以很好地使用 jreddit jar.
I recently created a sample android example which makes use of jreddit library. Haven't done much for the example, but the android app would work fine with the jreddit jar in it.
我花了一些时间才弄明白,但基本上这些是我为了使 android 应用工作而使用的依赖项,我认为它也应该对您有所帮助.
I took me a while to figure things out, but basically these are the dependencies that I've used in order to make the android app work and I think it should help you out as well.
另外,请注意我发布的版本 (1.0.3),它是最新的 jReddit 版本,其中包含一些有助于 Android 开发者解决问题的更改.
Also, pay attention to the version that I posted (1.0.3) which is the latest jReddit release which includes several changes that help android developers out.
应用插件:'com.android.application'
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "jreddit.github.com.jredditsampleproject"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/plexus/components.xml'
}
}
dependencies {
compile('com.github.jreddit:jreddit:1.0.3') {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude module: 'junit'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'com.android.support:appcompat-v7:22.0.0'
}
您还可以在此处查看完整示例一个>
这篇关于jReddit 和 Java.lang.NoClassDefFoundError 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!