我刚刚将一些项目导入到我的 Android Studio 中,并在最新版本的 android SDK 即 v7 上测试该应用程序,但是当我尝试在旧设备上测试该应用程序时,该应用程序无法正常工作,因为应用程序包含身份验证活动在 v7 上成功运行,但不能在旧的 android 设备上运行。
org.apache.httpcomponents:httpmime:4.5.1'
我注意到上面的依赖被忽略了。我知道在 API 23 之后,这种依赖会被贬值。有没有什么方法可以避免 gradle 的无知?因为我的整个应用程序都基于http 组件。
应用程序的 Gradle 文件是:
代码更新
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "25.0.0"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.creative.nerdzprocollegemanager"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-fr'] } }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'org.apache.httpcomponents:httpmime:4.5.1'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
应用功能是:
@Override
protected String doInBackground(Void...params) {
byte[] result;
String resultString = "";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
ArrayList < NameValuePair > nameValuePairs = new ArrayList < > ();
for (String key: postParams.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, postParams.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
errorMessage = "ok";
result = EntityUtils.toByteArray(response.getEntity());
resultString = new String(result, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
errorMessage = "Encoding is not supported";
} catch (Exception e) {
errorMessage = "An error occurred";
}
return resultString;
}
@Override
protected void onPostExecute(String s) {
if (errorMessage.equals("ok")) {
sourceActivity.requestFinished(s, requestTag);
} else
sourceActivity.requestFailed(errorMessage, requestTag);
}
}
有什么方法可以避免 gradle 对依赖项的无知?或者我应该使用一些替代 httpclient 的方法?请建议我一些解决方案,因为我对 android 没有太多经验。
将此行添加到 android 端的 gradle 文件中
android{
...
useLibrary 'org.apache.http.legacy'
...
}
编辑:也替换
compile group: 'cz.msebera.android' , name: 'httpclient', version:
'4.4.1.1'
和
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
或者
compile 'org.apache.httpcomponents:httpclient:4.5.1'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句