Note that there are some explanatory texts on larger screens.

plurals
  1. POApp crashing on network issue/server down
    primarykey
    data
    text
    <p>I am using Asynctask to extract some data from the web and use it in further calculations. I have implemented a connectivity check as well, it returns false if there is no connection but It crashes if there is internet connection and "the server is down". I want to display a dialog to notify the user that their internet is experiencing problems. But I can't seem to get it right. Please help. Thanks in advance. :)</p> <pre><code>HttpClient client; String URL, re, element; JSONObject json, getjson; int i, j, statusCode; HttpGet httpget; HttpResponse response; HttpEntity entity; InputStream is; BufferedReader reader; StringBuilder sb, sb1; SharedPreferences getinput, passjson; ProgressDialog pd; Context context = DataRetrieve.this; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.dataretrieve); // Bundle gotBasket = getIntent().getExtras(); // URL = gotBasket.getString("key"); getinput = getSharedPreferences("input", 0); URL = getinput.getString("key", null); if (!isNetworkReachable()) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("No Network Connection"); builder.setMessage("The Network is unavailable. Please check your internet connection."); builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Intent m = new Intent( android.provider.Settings.ACTION_SETTINGS); startActivity(m); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Intent n = new Intent(DataRetrieve.this, OpTilt.class); startActivity(n); } }); builder.create().show(); } else { Read r = new Read(); r.execute(); } } private boolean isNetworkReachable() { // TODO Auto-generated method stub ConnectivityManager mManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo current = mManager.getActiveNetworkInfo(); if (current != null &amp;&amp; current.isConnectedOrConnecting()) { return true; } return false; } public class Read extends AsyncTask&lt;String, Integer, String&gt; { @Override protected void onPreExecute() { pd = new ProgressDialog(DataRetrieve.this); pd.setTitle("Processing..."); pd.setMessage("Please wait."); pd.setCancelable(false); pd.setIndeterminate(true); pd.show(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub re = null; is = null; json = null; try { httpget = new HttpGet(URL); client = new DefaultHttpClient(); response = client.execute(httpget); entity = response.getEntity(); is = entity.getContent(); statusCode = response.getStatusLine().getStatusCode(); } catch (Exception e) { statusCode = -1; Log.e("log_tag", "Erro http " + e.toString()); } if (statusCode == 200) { try { reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); re = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Erro conversão " + e.toString()); } } return re; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub pd.dismiss(); try { json = new JSONObject(result); getjson = json.getJSONObject("Solar"); String H[] = new String[getjson.length()]; for (i = 0, j = 1; i &lt; getjson.length(); i++, j++) { H[i] = getjson.getString("" + j); } SharedPreferences sendHm; sendHm = getSharedPreferences("parsedata", 0); SharedPreferences.Editor editor = sendHm.edit(); for (int k = 0; k &lt; H.length; k++) { editor.putString("Hm" + k, H[k]); } editor.commit(); Intent f = new Intent(DataRetrieve.this, Calculator.class); startActivity(f); /* * Bundle bundle = new Bundle(); bundle.putStringArray("key1", * H); Intent f = new Intent(DataRetrieve.this, * Calculator.class); f.putExtras(bundle); startActivity(f); */ } catch (JSONException e) { Log.e("log_tag", "Erro dados " + e.toString()); } } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); finish(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload