Android studio 中NameValuePair跟BasicNameValuePair 不能正常導包問題

Android studio 中NameValuePair跟BasicNameValuePair 不能正常導包問題

針對一個post請求,android studio中沒法正常導入如下兩個包:android

 

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

Cannot resolve symbol NameValuePair

最後解決方案是:apache

在module文件中加入了less

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

dependencies {
    ...
    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpclient:4.5'
}

}

到此問題獲得解決,若是仍是報錯出現以下:ide

 compile 'org.apache.httpcomponents:httpclient:4.5.5'

出現紅色報錯線並提示下列錯誤:post

httpclient defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. less... (Ctrl+F1) this

There are a number of libraries that duplicate not just functionality of the Android platform but using the exact same class names as the ones provided in Android -- for example the apache http classes. This can lead to unexpected crashes.  To solve this, you need to either find a newer version of the library which no longer has this problem, or to repackage the library (and all of its dependencies) using something like the jarjar tool, or finally, rewriting the code to use different APIs (for example, for http code, consider using HttpUrlConnection or a library like okhttp).  Issue id: DuplicatePlatformClassesspa

解決辦法:code

在android中添加下列代碼:component

 configurations {
        all {
            exclude module: 'httpclient'
            exclude module: 'commons-logging'
        }
    }

截圖以下:orm

 

從新點擊出錯語句編譯便可解決。

相關文章
相關標籤/搜索