The application uses the following scheme:

  1. AsyncTask is created.
  2. The doInBackground() loading data from the server.
  3. Using try {} catch(IOException e){} a boot error is caught, if there is one.

The result is this: in the absence of the Internet, the application just crashes with an error ... Question: how to make the application not crash, but display a message about the connection problem?

UPD Code on which the application falls:

 try { items = json.getJSONArray("result"); } catch (JSONException e) { e.printStackTrace(); } 

Logcat:

Process: ru.mediaznak.test, PID: 6410 java.lang.NullPointerException at ru.mediaznak.testvk.MainActivity $ JSONParse.onPostExecute (MainActivity.java:271)

I understand that something is wrong in json

  • one
    Show the AsyncTask code. - Opalosolo
  • Vestimo falls not with IOExecption, or the work of the ketch is not where the error is - Chad
  • one
    and logCat show too. - Yura Ivanov
  • Crashes with an error ... which one? Show that in the catch block! doInBackground () in a try block? - MDJHD

1 answer 1

 final static int JSON_ERROR = 1; final static int NETWORK_ERROR = 2; final static int UNKNOWN_ERROR = 3; int error = 0; try { items = json.getJSONArray("result"); } catch (JSONException e) { error = JSON_ERROR; } catch(IOException e) { error = NETWORK_ERROR; } catch(Exception e) { error = UNKNOWN_ERROR; } //в onResult проверка error