In my application, I connect to the website to collect some information at startup using AsyncTask, using a catch attempt, from here I can display an error in my catlog if it is connected, but I tried to successfully display a dialog box with a failure message connection with parameters for reconnecting or shutting down, please check my code and tell me what I'm doing wrong, or an idea on how to execute this
//this is our download file asynctask class 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 @Override protected 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; } }
EDIT: then I will try to add the following code as suggested by Arun
catch (Exception e) {
but it doesnβt work, my application crashes in the if statement line in onPostExecute. I still need some help.
zvzej source share