2015-12-21 230 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(); 
    } 
} 

和比它採取位圖。