2015-09-07 19 views
0

我用來從下面的代碼JSON數據,但現在HttpClient棄用再也看不到了。請問你能幫幫我嗎?如何從JSON數據 - HttpClient的過時

String responseString = null; 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     URI apiCall = new URI("..."); 
     HttpGet request = new HttpGet(); 
     request.setURI(apiCall); 
     HttpResponse response = client.execute(request); 
     responseString = EntityUtils.toString(response.getEntity()); 
    } catch (Exception e) { 
     Log.e("TAG", "some sort of problem encountered", e); 
    } 
+1

https://stackoverflow.com/questions/32380439/namevaluepair-error-namevaluepair-cannot-be-resolved-to-a-type – CommonsWare

+0

感謝另一個類評論@CommonsWare,如果你寫一個答案,我會接受它是正確的。 但爲什麼如果我用https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html或http://developer.android.com/preview/behavior-changes.html編譯我的gradle #behavior-apache-http-client我的代碼不正確? – Pier

+0

@CommonsWare:看起來像你標記另一個重複問題的副本:-) – BNK

回答

0

使用像

URL url = new URL("http://www.android.com/"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    try { 
    InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
    readStream(in); 
    finally { 
    urlConnection.disconnect(); 
    } 
} 
+0

我該如何適應AsyncTask? – Pier