我的应用程序需要与服务器进行大量通信。有些电话需要很长时间,所以我需要显示进度对话框。之前,我说:
ProgressDialog pd;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(app);
pd.setOwnerActivity(app);
pd.setTitle("Идет загрузка...");
pd.setCancelable(true);
pd.show();
}
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
//some code
}
到每个扩展 asynctask 的类。但是,现在我必须在 asynctask 类上使用 .get() ,因此 progressdialog 仅在执行任务时才会闪烁。
之后,我尝试从 asynctask 类中删除 progressdialog 调用并将其放入 JsonParser:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jObj1 = null;
static String json = "";
DefaultHttpClient httpClient = null;
static Context context;
ProgressDialog pd;
// constructor
public JSONParser() {
}
private static Boolean isOnline() {
context = UILApplication.getAppContext();
Log.d("CONTEXT",context.toString());
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.d("Conected", "Conected");
return true;
}
}
}
}
Log.d("disconected", "disconected");
return false;
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
if (isOnline()) {
pd = new ProgressDialog(context);
//pd.setOwnerActivity(context);
pd.setTitle("Идет загрузка...");
pd.setCancelable(true);
pd.show();
try {
String u = url;
u = u + "?";
httpClient = HttpClientFactory.getThreadSafeClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
for (int i = 0; i < params.size(); i++) {
u = u + params.get(i).getName() + "="
+ params.get(i).getValue() + "&";
}
Log.d("your url is", u);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("data is sent", "true");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Log.d("wait", "true");
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
if (json.contains("error:2")) {
return null;
}
Log.d("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
return null;
}
// 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 null;
}
pd.dismiss();
return jObj;
}
pd.dismiss();
error();
return null;
}
}
不幸的是,它给了我错误。据我了解,我应该在 UIthread 中调用对话框。
06-20 13:46:01.368: W/System.err(1244): java.util.concurrent.ExecutionException: java.lang.RuntimeException: 无法在未调用 Looper.prepare() 的线程内创建处理程序 06- 20 13:46:01.368: W/System.err(1244): 在 java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223) 06-20 13:46:01.377: W/System.err( 1244): 在 java.util.concurrent.FutureTask.get(FutureTask.java:82) 06-20 13:46:01.377: W/System.err(1244): 在 android.os.AsyncTask.get(AsyncTask.java :482) 06-20 13:46:01.377: W/System.err(1244): 在 ua.mirkvartir.android.frontend.LoginActivity$2.onClick(LoginActivity.java:108) 06-20 13:46:01.377: W/System.err(1244): 在 android.view.View.performClick(View.java:4084) 06-20 13:46:01.377: W/System.err(1244): 在 android.view.View$PerformClick .run(View.java:16966) 06-20 13:46:01.387:W/System.err(1244): 在 android.os.Handler.handleCallback(Handler.java:615) 06-20 13:46:01.387: W/System.err(1244): 在 android.os.Handler.dispatchMessage (Handler.java:92)06-20 13:46:01.387:W / System.err(1244):在android.os.Looper.loop(Looper.java:137)06-20 13:46:01.387:W /System.err(1244): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 06-20 13:46:01.397: W/System.err(1244): 在 java.lang.reflect.Method。 invokeNative(本机方法)06-20 13:46:01.397:W/System.err(1244):在 java.lang.reflect.Method.invoke(Method.java:511)06-20 13:46:01.397:W /System.err(1244): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 06-20 13:46:01.397: W/System.err(1244): 在 com. android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-20 13:46:01.407:W/系统。错误(1244):在 dalvik.system.NativeStart.main(本机方法)06-20 13:46:01.407:W/System.err(1244):原因:java.lang.RuntimeException:无法在内部创建处理程序尚未调用 Looper.prepare() 的线程 06-20 13:46:01.418: W/System.err(1244): at android.os.Handler.(Handler.java:121) 06-20 13:46:01.418 : W/System.err(1244): 在 android.app.Dialog.(Dialog.java:107) 06-20 13:46:01.418: W/System.err(1244): 在 android.app.AlertDialog.( AlertDialog.java:114) 06-20 13:46:01.427: W/System.err(1244): 在 android.app.AlertDialog.(AlertDialog.java:98) 06-20 13:46:01.427: W/System .err(1244): 在 android.app.ProgressDialog.(ProgressDialog.java:77) 06-20 13:46:01.427: W/System.err(1244): 在 ua.mirkvartir.android.frontend.adapter.JSONParser .getJSONFromUrl(JSONParser.java:79) 06-20 13:46:01.437:W/系统。错误(1244):在 ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:64)06-20 13:46:01.437:W/System.err(1244):在 ua.mirkvartir.android.frontend .LoginActivity$Login.doInBackground(LoginActivity.java:280) 06-20 13:46:01.437: W/System.err(1244): at ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java: 1) 06-20 13:46:01.447: W/System.err(1244): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46:01.447: W/System.err (1244): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.447: W/System.err(1244): 在 java.util.concurrent.FutureTask。运行(FutureTask.java:137)06-20 13:46:01.457:W/System.err(1244):在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)06-20 13:46 :01.457: W/System.err(1244): 在 java。util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 06-20 13:46:01.457: W/System.err(1244): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569 ) 06-20 13:46:01.457: W/System.err(1244): 在 java.lang.Thread.run(Thread.java:856) 06-20 13:46:01.457: D/得到响应 (1244) : true 06-20 13:46:01.467: D/work done(1244): true 06-20 13:46:01.477: W/dalvikvm(1244): threadid=11: 线程退出但未捕获异常 (group=0x40a13300) 06-20 13:46:01.517:E/AndroidRuntime(1244):致命异常:AsyncTask #1 06-20 13:46:01.517:E/AndroidRuntime(1244):java.lang.RuntimeException:执行 doInBackground 时出错() 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 android.os.AsyncTask$3.done(AsyncTask.java:299) 06-20 13:46:01.517: E/AndroidRuntime(1244):在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent.FutureTask.setException(FutureTask.java: 124) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 06-20 13:46:01.517: E/AndroidRuntime( 1244): 在 java.util.concurrent.FutureTask.run(FutureTask.java:137) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask. java:230) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 06-20 13:46:01.517: E/AndroidRuntime( 1244): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 06-20 13:46:01.517:E/AndroidRuntime(1244): at java.lang.Thread.run(Thread.java:856) 06-20 13:46:01.517: E/AndroidRuntime(1244): 由: java.lang.RuntimeException: 不能在尚未调用 Looper.prepare() 的线程内创建处理程序 06-20 13:46:01.517: E/AndroidRuntime(1244): at android.os.Handler.(Handler.java:121) 06-20 13:46: 01.517: E/AndroidRuntime(1244): 在 android.app.Dialog.(Dialog.java:107) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 android.app.AlertDialog.(AlertDialog.java :114) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 android.app.AlertDialog.(AlertDialog.java:98) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在android.app.ProgressDialog.(ProgressDialog.java:77) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.adapter.JSONParser.getJSONFromUrl(JSONParser.java:79) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:64) 06-20 13:46:01.517: E/AndroidRuntime( 1244): 在 ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:280) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.LoginActivity $Login.doInBackground(LoginActivity.java:1) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46: 01.517: E/AndroidRuntime(1244): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06 -20 13:46:01.857: W/EGL_emulation(1244): eglSurfaceAttrib 未实现UserFunctions.loginUser(UserFunctions.java:64) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:280) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:1) 06-20 13:46:01.517: E/AndroidRuntime(1244):在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305 ) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244): eglSurfaceAttrib 未实现UserFunctions.loginUser(UserFunctions.java:64) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:280) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:1) 06-20 13:46:01.517: E/AndroidRuntime(1244):在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305 ) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244): eglSurfaceAttrib 未实现doInBackground(LoginActivity.java:280) 06-20 13:46:01.517: E/AndroidRuntime(1244): at ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:1) 06-20 13: 46:01.517: E/AndroidRuntime(1244): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent。 FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244): eglSurfaceAttrib未实现doInBackground(LoginActivity.java:280) 06-20 13:46:01.517: E/AndroidRuntime(1244): at ua.mirkvartir.android.frontend.LoginActivity$Login.doInBackground(LoginActivity.java:1) 06-20 13: 46:01.517: E/AndroidRuntime(1244): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 06-20 13:46:01.517: E/AndroidRuntime(1244): 在 java.util.concurrent。 FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244): eglSurfaceAttrib未实现concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244) : eglSurfaceAttrib 未实现concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.517: E/AndroidRuntime(1244): ... 5 更多 06-20 13:46:01.857: W/EGL_emulation(1244) : eglSurfaceAttrib 未实现
所以我想知道显示进度对话框的方式,它会在我需要的地方弹出。我可以在调用 getJsonFromUrl 时显示它并在 getJsonFromUrl 返回时关闭它吗?