如何避免在android中忽略依赖?

时间:2017-06-08 04:38:05

标签: android gradle httpurlconnection

我刚刚将一些项目导入我的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没有太多经验。

1 个答案:

答案 0 :(得分:0)

在side 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'