使用apache HTTP libs运行App时出错

时间:2014-12-31 12:21:52

标签: android-studio apache-httpclient-4.x

从AndroidStudio在模拟器上运行我的应用程序时出现致命错误:

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicLineFormatter; in class Lorg/apache/http/message/BasicLineFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicLineFormatter' appears in /system/framework/ext.jar)

这是我的build.gradle模块:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.htw.roishachor.dbplanupdater"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

我的代码:

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.*;
import org.apache.http.client.methods.HttpGet;


public void myMethod() {
       CloseableHttpResponse response = null;
        try{
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpget = new HttpGet(new URI(INITIAL_URL));
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); ....

奇怪的是。我不知道它是否与它有任何关系 - 我的libs和build文件夹在AndroidStudio中不可见,我只能在资源管理器中看到它们。 我在教程中看到,我必须右键单击每个.jar文件并将其添加为库。 它与它有什么关系吗?罐子在我的lib文件夹中,但如上所述,我只能通过我的浏览器添加它们

1 个答案:

答案 0 :(得分:0)

审核文档here

将这些chgs设为gradle.build并将mavenCentral添加到顶级gradle.build ....

allprojects {
    repositories {
        mavenCentral()
    }
}

然后检查名称空间冲突,您必须使用apache版本(HC4 suffx)

import org.apache.http.client.methods.HttpGetHC4;
import org.apache.http.client.methods.HttpPostHC4;
import org.apache.http.client.methods.HttpPutHC4;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.BufferedHttpEntityHC4;
import org.apache.http.entity.ByteArrayEntityHC4;

进行这些更改后,您应该得到预测结果。