java - Android 应用程序上缺少 org/apache/http/client/methods/HttpUriRequest

标签 java android httpclient

我正在尝试从我的 Android 应用程序使用 Telegrambot(由于某些项目要求,为 4.4)。我实现了一个简单的机器人,但是当我尝试运行它时,出现以下错误:

java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest

    at com.mypackage.Model.communication.remote.telegram.TelegramBotTest.initialize(TelegramBotTest.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我尝试过

compile 'org.apache.httpcomponents:httpclient-android:4.3.3'

但没有成功。我必须包含什么正确的依赖项?这些是我为支持电报而包含的依赖项:

// *** Telegram dependencies ***
compile 'org.json:org.json:chargebee-1.0'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.6'
compile 'org.telegram:telegrambots:2.4.4.5'

--更新: 我还收到这些警告:

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies]
Warning:WARNING: Dependency org.json:json:20160810 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.3 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160810 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.3 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160810 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.3 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160810 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.3 is ignored for release as it may be conflicting with the internal version provided by Android.

最佳答案

我已经按照这个方法解决了。关于依赖项,我添加了以下内容:

// *** Telegram dependencies ***
compile 'org.json:org.json:chargebee-1.0'
compile 'org.apache.httpcomponents:httpcore:4.4.6'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
compile files('libs/commons-logging-1.2.jar')    
compile "org.telegram:telegrambots:3.3"

在 android 部分我添加了这个:

configurations {
    compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
    compile.exclude group: "commons-logging", module: "commons-logging"
}

现在,我可以执行 TelegramBot 代码而不会出现运行时异常。

关于java - Android 应用程序上缺少 org/apache/http/client/methods/HttpUriRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46283831/

相关文章:

java - py4J 在多线程java上获取新的通信 channel 时出错

java - 在 JPA 2.0 中捕获约束违规

android - 如何从 res/font 目录链接 android/assets 文件夹中的 css 文件中的字体

android自定义对话框背景

Android模拟器与PC模拟器进行通信

安卓 : Share session between Webview and httpclient

java - 如何循环这个if语句?

java - 下载一个完整的网页,包括 java 中的资源(如图像)

android - 游戏代理 : Volley error when reporting played

c# - 使用 Mock HttpClient 进行 Mock Refit SDK 注入(inject)以进行集成测试