2015-12-21 225 views
0

那么,我的ImageDownloader类是与新的Sdk打破。我现在怎么去做这项工作?任何可能我只需要替换下面的一些类而不对我的代码做重大更改?在gradle这个文件AndroidHttpClient已弃用

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' 

或本:

static Bitmap downloadBitmap(String url) { 
     final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
     final HttpGet getRequest = new HttpGet(url); 

     try { 
      HttpResponse response = client.execute(getRequest); 
      final int statusCode = response.getStatusLine().getStatusCode(); 
      if (statusCode != HttpStatus.SC_OK) { 
       Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
       return null; 
      } 

      final HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       InputStream inputStream = null; 
       try { 
        inputStream = entity.getContent(); 
        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
        return bitmap; 
       } finally { 
        if (inputStream != null) { 
         inputStream.close(); 
        } 
        entity.consumeContent(); 
       } 
      } 
     } catch (Exception e) { 
      // Could provide a more explicit error message for IOException or IllegalStateException 
      getRequest.abort(); 
      Log.w("ImageDownloader", "Error while retrieving bitmap from " + url); 
     } finally { 
      if (client != null) { 
       client.close(); 
      } 
     } 
     return null; 
    } 
+1

到底什么是坏,什么错误,你现在接受? – Sam

+1

你为什么曾经使用过'AndroidHttpClient'?这是字面上古老的,它已被推荐多年。仅在最早的Android版本中。你应该很久以前取代它了。无论如何,为什么大惊小怪?这只是一种方法。重写它应该很简单。你到目前为止尝试了什么?你为什么不能自己做? –

回答

1

您可以使用此

android { 
    compileSdkVersion 23 
    buildToolsVersion '23.0.2' 
    useLibrary 'org.apache.http.legacy' //<-this line only 

或者你可以更新代码,并使用HttpURLConnection这样的:

URL url = new URL("your_url"); 
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
httpURLConnection.setReadTimeout(10000); 
httpURLConnection.setConnectTimeout(15000); 
httpURLConnection.setRequestMethod("POST"); 
httpURLConnection.setDoInput(true); 
httpURLConnection.setDoOutput(true); 
setupDataToDB(); 
OutputStream outputStream = httpURLConnection.getOutputStream(); 
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); 
bufferedWriter.write(StringGenerator.queryResults(nameValuePairs)); 
bufferedWriter.flush(); 
bufferedWriter.close(); 
outputStream.close(); 
httpURLConnection.connect(); 
InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream()); 

希望它有帮助!

1

您可以采取的InputStream:

HttpURLConnection urlConnection = null; 
try { 
    URL url = new URL(path); 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setConnectTimeout(CONNECTON_TIMEOUT_MILLISECONDS); 
    urlConnection.setReadTimeout(CONNECTON_TIMEOUT_MILLISECONDS); 
    inputStream = urlConnection.getInputStream(); 
    //add code to take bitmap here 
} finally { 
    if (urlConnection != null) { 
     urlConnection.disconnect(); 
    } 
} 

和比它采取位图。