我正在使用 JSONparser 在远程 mySQL 数据库中获取一些值,并且在我在 Android 3.0+(Honeycomb)中对其进行测试之前它工作正常,它具有不允许进程在其主线程上执行网络操作的 Guardian .
所以我发现我需要一个Asynctask:如何修复 android.os.NetworkOnMainThreadException?
并尝试了这个主题:
好吧,现在我知道 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;
}
(...)
我试图将值传递给 var(JSONObject 结果),但没有效果。(NullPointerException,我猜它会在异步完成之前尝试访问 var,因为它是“异步”)
我需要做什么才能获得价值?有任何想法吗?
非常感谢你们!