Android 在doInBackground方法中启动活动
在下面的代码中,我从internet下载json,并希望在列表中显示。 如果列表为空,则转到其他活动,但其他活动未启动。 没有错误,但没有启动活动。 谢谢你的帮助Android 在doInBackground方法中启动活动,android,android-asynctask,Android,Android Asynctask,在下面的代码中,我从internet下载json,并希望在列表中显示。 如果列表为空,则转到其他活动,但其他活动未启动。 没有错误,但没有启动活动。 谢谢你的帮助 package ir.mohammadi.android.nightly; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.
package ir.mohammadi.android.nightly;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class AllNotes extends ListActivity {
ProgressDialog pDialog;
ArrayList<HashMap<String, String>> noteList;
JSONArray notes = null;
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR_MSG = "error_message";
private static String KEY_NOTE_ID = "note_id";
private static String KEY_NOTE_SUBJECT = "note_subject";
private static String KEY_NOTE_DATE = "note_date";
private static String EXECUTE_RESULT = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_list);
noteList = new ArrayList<HashMap<String, String>>();
new LoadAllNotes().execute();
ListView lv = getListView();
}
public class LoadAllNotes extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllNotes.this);
pDialog.setMessage("لطفا صبر کنید...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
UserFunctions userFunctions = new UserFunctions();
JSONObject jSon = userFunctions.getAllNotes("1");
Log.i("AllNotes >> jSon >>", jSon.toString());
try {
String success = jSon.getString(KEY_SUCCESS);
if (success == "1") {
notes = jSon.getJSONArray("notes");
for (int i = 0; i < notes.length(); i++) {
JSONObject c = notes.getJSONObject(i);
String id = c.getString(KEY_NOTE_ID);
String subject = c.getString(KEY_NOTE_SUBJECT);
String date = c.getString(KEY_NOTE_DATE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_NOTE_ID, id);
map.put(KEY_NOTE_SUBJECT, subject);
map.put(KEY_NOTE_DATE, date);
noteList.add(map);
}
} else {
finish();
Toast.makeText(getApplicationContext(),
jSon.getString(KEY_ERROR_MSG), Toast.LENGTH_SHORT).show();
Log.i("AllNotes >> No nightly >>", "...");
Intent i = new Intent(getApplicationContext(), login.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(AllNotes.this,
noteList, R.layout.list_item, new String[] {
KEY_NOTE_ID, KEY_NOTE_SUBJECT,
KEY_NOTE_DATE }, new int[] {
R.id.list_lbl_id, R.id.list_lbl_subject,
R.id.list_lbl_date });
setListAdapter(adapter);
}
});
}
}
}
这很棘手。一些需要注意的事情:
Intent i = new Intent(getApplicationContext(), login.class);
确保登录类的类型在清单中。通常通过类的名称引用类,而不是实例化的变量。老实说,这没什么区别,但值得一试
有时,你会在不同的环境下产生奇怪的副作用。我认为星际触觉是一种对这非常敏感的感觉。您可以尝试AllNotes.this以获取正在运行的活动的上下文。这很可能需要在UI线程上运行,因此如果这不起作用,您可以尝试runOnUiThread()。问题最可能的原因是以下几行:
String success = jSon.getString(KEY_SUCCESS);
您没有发布它,但我敢打赌在最后一行下面有一个JSONException堆栈跟踪<如果找不到密钥,并且在您注销的JSON数据中没有“success”密钥,则code>getString()将引发异常。该异常导致您在事后编写的所有代码根本没有被调用,这就是为什么您看不到任何事情发生的原因
还有几件事需要注意:
success==“1”
不是实现字符串相等的正确方法。=
运算符检查对象相等性(相同对象),如果要检查两个字符串是否相同,请使用ussuccess.equals(“1”)
onPostExecute()
。您不必从方法调用runOnUiThread()
,这是多余的Toast.makeText()
行将失败,因为您无法显示来自主线程以外的线程的Toast一般来说,最好在主线程上执行所有UI操作,作为设计点,您应该将任何操纵或更改屏幕的代码移动到
onPostExecute()
,以便在正确的时间调用它。这就是它的作用。我怎么能发现jSon中存在KEY_SUCCESS?我通过:jSon.has(KEY_SUCCESS)
来实现这一点,并正确工作,谢谢。您也可以使用optString()
,如果密钥不存在,它只会返回null,而不是完全爆炸。
String success = jSon.getString(KEY_SUCCESS);