我的應用程序需要很多與服務器的通信。一些電話需要很長時間,所以我需要顯示progressdialogs。 Befor,我把:如何彙總progressdialog?
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;
}
}
不幸的是它gaves我的錯誤。據我瞭解,我應該在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):at java.util.concurrent.FutureTask $ Sync.innerGet(FutureTask.java:223) 06-20 13 :46/01.377:W/System.err(1244):at java.util.concurrent.FutureTask.get(FutureTask.java:82)06-20 13:46:01.377:W/System.err(1244) :at android.os.AsyncTask.get(AsyncTask.java:482)06-20 13:46:01.377: W/System.err(1244):at ua.mirkvartir.android.frontend.LoginActivity $ 2.onClick(LoginActivity.java:108) 06-20 13:46:01.377:W/System.err(1244):at android.view.View.performClick(View .java:4084)06-20 13:46:01.377: W/System.err(1244):at android.view.View $ PerformClick.run(View.java:16966)06-20 13:46: 01.387:W/System.err(1244):at android.os.Handler.handleCallback(Handler.java:615)06-20 13:46:01.387:W/System.err(1244):at android。 os.Handler.dispatchMessage(Handler.java:92)06-20 13:46:01.387:W/System.err(1244):at android.os.Looper.loop(Looper.java:137)06-20 13:46:01.387: W/System.err(1244):at android.app。 ActivityThread.main(ActivityThread.java:4745)06-20 13:46:01.397:W/System.err(1244):at java.lang.reflect.Method.invokeNative(Native Method)06-20 13: System.err(1244):46/01.397:W/System.err(1244):at java.lang.reflect.Method.invoke(Method.java:511)06-20 13:46:01.397: :在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 06-20 13:46:01.397:W/System.err(1244):at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:553)06-20 13:46:01.407:W/System.err(1244):at dalvik.system.NativeStart.main(Native Method)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):at android.app.Dialog。(Dialog.java:107) 06-20 13:46:01.418:W/System.err(1244): at android.app.AlertDialog。(AlertDialog.java:114)06-20 13:46:01.427:W/System.err(1244):at android.app.AlertDialog。(AlertDialog.java:98)06 -20 13:46:01.427:W/System.err(1244):at android.app.ProgressDialog。(ProgressDialog。java:77)06-20 13:46:01.427:W/System.err(1244):at ua.mirkvartir.android.frontend.adapter.JSONParser.getJSONFromUrl(JSONParser.java:79) 06-20 13 :46:01.437:W/System.err(1244):at ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:64) 06-20 13:46:01.437:W/System.err( 1244):at 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):at android.os.AsyncTask $ 2.call(AsyncTask.java: 287)06-20 13:46:01.447: W/System.err(1 244):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 06-20 13:46:01.447:W/System.err(1244):at java.util.concurrent。 FutureTask.run(FutureTask.java:137)06-20 13:46:01.457:W/System.err(1244):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)06- 20 13:46:01.457:W/System.err(1244):at 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):at java.lang.Thread .run(Thread.java:856)06-20 13:46:01.457:D /得到 響應(1244):真06-20 13:46:01.467:完成D /工作(1244):真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):at android.os.AsyncTask $ 3.done(AsyncTask.java:299)06-20 13 :46/01.517: E/AndroidRuntime(1244):at 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):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask。 java:307) 06-20 13:46:01.517:E/AndroidRuntime(1244):at java.util.concurrent.FutureTask.run(FutureTask.java:137)06-20 13:46:01.517:E/AndroidRuntime(1244):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)06-20 13:46:01.517:E/AndroidRuntime(1244):at java.util.concurrent。 ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 06-20 13:46:01.517:E/AndroidRuntime(1244):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569)E/AndroidRuntime(1244):06-20 13:46:01.517:E/AndroidRuntime(1244):at java.lang.Thread.run(Thread.java:856)06-20 13:46:01.517: 導致: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):at android.app.Dialog。(Dialog.java:107) 06-20 13:46:01.517:E/AndroidRuntime(1244):at android.app.AlertDialog。(AlertDialog.java:114)06-20 13:46:01.517:E/AndroidRuntime(1244):at android.app.AlertDialog。(AlertDialog.java :98)06-20 13:46:01。517:E/AndroidRuntime(1244):at android.app.ProgressDialog。(ProgressDialog.java:77)06-20 13:46:01.517:E/AndroidRuntime(1244):at ua.mirkvartir.android.frontend .adapter.JSONParser.getJSONFromUrl(JSONParser.java:79) 06-20 13:46:01.517:E/AndroidRuntime(1244):at ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:64) 06-20 13:46:01.517:E/AndroidRuntime(1244):at ua.mirkvartir.android.frontend.LoginActivity $ Login.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):at 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沒有實現
所以我想知道顯示進度對話框的方式,無論我需要它,它都會彈出。當getJsonFromUrl被調用時我可以顯示它,當getJsonFromUrl返回時可以解除它嗎?
如果有幫助,不要忘記打勾 – amalBit