在我的应用程序中,我连接到一个网站,在开始时收集一些AsyncTask的信息,使用try catch,从这里我可以在我的catlog中显示错误,如果有任何连接,但我一直试图运气好显示一个对话框显示连接失败以及重新连接或退出的选项,请检查我的代码并告诉我我做错了什么或者想知道如何实现这一点
//this is our download file asynctaskclass DownloadfileAsync extends AsyncTask<String, String, String> { @OverrIDe protected voID onPreExecute() { super.onPreExecute(); showDialog(DIALOG_DOWNLOAD_PROGRESS); } @OverrIDe protected String doInBackground(String... aurl) { try { String result = ""; try { httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); httpPost httppost = new httpPost("http://mywebsiteaddress"); // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpResponse response = httpclIEnt.execute(httppost); httpentity entity = response.getEntity(); inputStream webs = entity.getContent(); // convert response to string try { BufferedReader reader = new BufferedReader( new inputStreamReader(webs, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } webs.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } // parse Json data try { JsONArray jArray = new JsONArray(result); for (int i = 0; i < jArray.length(); i++) { JsONObject Json_data = jArray.getJsONObject(i); webResult resultRow = new webResult(); //infotodownload arrayOfWebData.add(resultRow); } } catch (JsONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } catch (Exception e) { // this is the line of code that sends a real error message to the // log Log.e("ERROR", "ERROR IN CODE: " + e.toString()); // this is the line that prints out the location in // the code where the error occurred. e.printstacktrace(); } return null; } protected voID onProgressUpdate(String... progress) { Log.d(LOG_TAG,progress[0]); mProgressDialog.setProgress(Integer.parseInt(progress[0])); } @OverrIDe protected voID onPostExecute(String unused) { //dismiss the dialog after the file was downloaded dismissDialog(DIALOG_DOWNLOAD_PROGRESS); }}//our progress bar settings@OverrIDeprotected Dialog onCreateDialog(int ID) { switch (ID) { case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0 mProgressDialog = new ProgressDialog(this); mProgressDialog.setTitle("Conectando al ServIDor"); mProgressDialog.setMessage("Cargando informacion..."); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressstyle(ProgressDialog.STYLE_SPINNER); mProgressDialog.setCancelable(true); mProgressDialog.show(); return mProgressDialog; default: return null; }}
编辑:
然后我尝试添加Arun建议的下一个代码
catch (Exception e) { // this is the line of code that sends a real error message to the // log Log.e("ERROR", "ERROR IN CODE: " + e.toString()); // this is the line that prints out the location in // the code where the error occurred. e.printstacktrace(); return "ERROR_IN_CODE"; } return null; // if I place here return "ERROR_IN_CODE" it calls the dialog but it gets always called so I don't need it here } @OverrIDe protected voID onPostExecute(String unused) { //dismiss the dialog after the file was downloaded dismissDialog(DIALOG_DOWNLOAD_PROGRESS); if(unused.equals("ERROR_IN_CODE")){ //I get a system crash here! errornote(); } }}public voID errornote() { AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setMessage("No se a podIDo descargar la informacion de los medios, deseas reintentarlo, o salir?").setCancelable(false) .setPositivebutton("Conectar de Nuevo", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { new DownloadfileAsync().execute(); } }) .setNegativebutton("Salir", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { // Action for 'NO' button finish(); } }); AlertDialog alert = alt_bld.create(); // Title for AlertDialog alert.setTitle("Error en la Conexion!"); // Icon for AlertDialog alert.setIcon(androID.R.drawable.ic_dialog_alert); alert.show();}
但是也没有工作,我的应用程序崩溃在onPostExecute的if语句行中.我还需要帮助.
解决方法:
因为从受保护的字符串doInBackground(String … aurl)返回一个String对象,所以从catch块返回一些自定义错误字符串,并在protected voID onPostExecute(String unused)中访问它.检查返回的String对象是否为自定义错误字符串,并在protected voID onPostExecute(String unused)中显示该对话框,但仅在解除progressDialog之后,即在此行dismissDialog之后(DIALOG_DOWNLOAD_PROGRESS);显示错误对话框.
编辑
当控件进入Catch块时,返回一些简单的String,就像你使用的那个“ERROR_IN_CODE”一样.
catch (Exception e) { // this is the line of code that sends a real error message to the // log Log.e("ERROR", "ERROR IN CODE: " + e.toString()); // this is the line that prints out the location in // the code where the error occurred. e.printstacktrace(); return "ERROR_IN_CODE";}
并在onPostExecute(String unused)中检查以下内容
protected voID onPostExecute(String unused) { //dismiss the dialog after the file was downloaded dismissDialog(DIALOG_DOWNLOAD_PROGRESS); if(unused != null && unused.equals("ERROR_IN_CODE")){ showDialog(SOME_DIALOG_TO_SHOW_ERROR); }}
总结 以上是内存溢出为你收集整理的java – Android,如何从try的错误中显示一个对话框?全部内容,希望文章能够帮你解决java – Android,如何从try的错误中显示一个对话框?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)