現在我使用的HttpClient它被廢棄了! :-( 所以我想用HttpURLConnection類.. 這裏是我的代碼:使用URLConnection的,而不是HttpClient的與JSONArray
public JSONArray GetDetails(int ID){
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(StaticVariables.url_for_details + ID);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
有人可以幫助我使用HttpURLConnection類,而不是HttpClient的,所以我可以用jsonarray此外
你面臨的問題是什麼?或者特別是你不明白/知道阻止你使用HttpUrlConnection? – Djizeus
我不知道在哪個方式我必須使用HttpUrlConnection在我的代碼 – fabiruu111