這段代碼適用於較老的api版本,例如。我已經在API8-2.2上使用它,並且它做我想要的。如何使JSONParser類適用於Android ICS和更新版本?
但是,當試圖運行與Android 4.2.1我的手機上的應用程序,它只是墜毀。我試圖在互聯網上發現這個問題,發現我需要Asyntask,所以我開始尋找它,但我仍然不明白。有人可以幫我解決這個問題嗎?
package com.thevnkid93.ucebnice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jArr=null;
static String json = "";
// function get json from url
// by making HTTP POST or GET method
public static JSONArray makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jArr = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String (Array)
return jArr;
}
}
的logcat:
02-23 23:01:10.540:E/AndroidRuntime(30520):android.os.NetworkOnMainThreadException
檢查LogCat以查看Java堆棧跟蹤告訴您有關「崩潰」的信息。 – CommonsWare 2013-02-22 20:24:01
02-23 23:01:10.540:E/AndroidRuntime(30520):android.os.NetworkOnMainThreadException – vnkid 2013-02-23 22:05:00
如異常所示,您正嘗試在主應用程序線程上執行網絡I/O。請按照Raghav Sood的建議,將此I/O移至後臺線程,例如'AsyncTask'。 – CommonsWare 2013-02-24 01:35:03