我用JSONparser來獲得遠程MySQL數據庫的一些值,這是工作的罰款,直到我在Android 3.0以上版本(蜂窩)進行了測試,有的監護人這將不允許進程在其主線程上執行聯網操作。的AsyncTask + JSON(如何得到一些值)
所以我發現我需要一個的AsyncTask:How to fix android.os.NetworkOnMainThreadException?
並試用了此主題:
Get returned JSON from AsyncTask
Return JSON Array from AsyncTask
How to return data from asynctask
好了,現在我知道的AsyncTask不能返回值,但我需要的,因爲我有很多來電在不同的活動等待,要處理和/或顯示在屏幕上的信息來獲得此JSON值(這就是爲什麼我不能在OnPostExecute中實現,我猜)。
下面是這些類,以及我的舊JsonParser的一些改編。
JSONParser.java
public class JSONParser extends AsyncTask<List<NameValuePair>, Void, JSONObject> {
static InputStream is = null;
static JSONObject jObj = null;
static JSONObject result = null;
static String json = "";
private static String jsonURL = "http://192.168.1.119/Server/db/json/";
private ProgressDialog progressDialog;
// constructor
public JSONParser() {
}
@Override
protected void onPreExecute() {
//progressDialog = new ProgressDialog();
progressDialog.setMessage("Aguarde...");
progressDialog.show();
}
protected JSONObject doInBackground(List<NameValuePair>... params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(jsonURL);
httpPost.setEntity(new UrlEncodedFormEntity(params[0]));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception 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();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
protected void onPostExecute(JSONObject jObj) {
result = jObj;
progressDialog.dismiss();
}
public JSONObject getJson(List<NameValuePair> params){
this.execute(params);
return result;
}
}
UserFunctions.java(有的叫例子)
(...)
public UserFunctions(){
JSONParser jsonParser = new JSONParser();
}
public JSONObject loginUser(String email, String password){
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJson(params);
//JSONObject json = jsonParser.execute(params); //doesn't return
return json;
}
public JSONObject listProjects(String email){
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", list_projects));
params.add(new BasicNameValuePair("email", email));
JSONObject json = jsonParser.getJson(params);
return json;
}
public JSONObject setDisp(String dispID, String disp, String comment){
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", set_disp));
params.add(new BasicNameValuePair("dispID", dispID));
params.add(new BasicNameValuePair("disp", disp));
params.add(new BasicNameValuePair("comment", comment));
JSONObject json = jsonParser.getJson(params);
return json;
}
(...)
我試圖讓值傳遞給一個變種(JSONObject的結果),但沒有效果。 (NullPointerException,我想它嘗試在異步結束之前訪問var,因爲它是「異步」)
我需要做什麼來獲取值?有任何想法嗎?
非常感謝的傢伙!
你可以發佈stacktrace嗎? – Raghunandan
[看到這個答案](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648)與AsyncTask一起使用'interface' '。然後,您可以在任務完成時得到回調。 – codeMagic
你也可以使用arraylist。這很容易存儲和檢索值。 – Ranjit