【问题标题】:cant resolve httpclient, httppost, http reponse and NameValuePair无法解析 httpclient、httppost、http 响应和 NameValuePair
【发布时间】:2015-11-10 06:05:16
【问题描述】:

我知道它们在 api-23 上不再可用,但我从过去 10 小时开始尝试添加库,以便我可以使用我以前的代码,但如果有人帮助我升级我以前的代码(如 httpclient),它仍然无法在我这边工作到 httpURLConnenction

        protected Boolean doInBackground(String... urls) {
        for (String url : urls) {
            try {

                ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
                pairs.add(new BasicNameValuePair("btnSubmit", updateTrigger));
                pairs.add(new BasicNameValuePair("total", ""+total));

                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(url);
                post.setEntity(new UrlEncodedFormEntity(pairs));
                HttpResponse response = client.execute(post);
                is1 = response.getEntity().getContent();

            } catch (ClientProtocolException e) {
                error = "ClientProtocolException: " + e.getMessage();
                return false;
            } catch (IOException e) {
                error = "ClientProtocolException: " + e.getMessage();
            }
        }

        return true;
    }

这是我的 build.gradle 文件

 apply plugin: 'com.android.application'


android {

compileSdkVersion 23
buildToolsVersion "23.0.2"



defaultConfig {
    applicationId "com.androidtraining.practice2"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),         'proguard-rules.pro'
    }
  }
  }

    dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'


}

我将这些添加到我的 build.gradle 文件中,例如

       compile 'org.apache.httpcomponents:httpcore:4.4.1'
       compile 'org.apache.httpcomponents:httpclient:4.5'
      compile 'com.github.jalasoft:HttpClient:1.0.5'
      compile "cz.msebera.android:httpclient:4.4.1.1"

从上面的图书馆 cz.msebera 几乎可以帮助我删除我的所有错误,然后在行得到一个转换错误

         post.setEntity(newUrlEncodedFormEntity(pairs)); 

所以它也没有帮助..所以提前 thnks ..

【问题讨论】:

    标签: apache http-post httpclient httpresponse


    【解决方案1】:

    如果继续使用 Apache HTTP API,您必须首先在 build.gradle 文件中声明以下编译时依赖项:

    android {
        useLibrary 'org.apache.http.legacy'
    }
    

    请阅读以下内容:

    Apache HTTP Client Removal

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多