Volley onErrorResponse doesn't work when no internet access

713 Views Asked by At

I have a function getTeacherData() and it returns a json object,The problem is when i disable my internet connection on my phone the app crashes(android doesn't show the error in android monitor,it just crashes without showing the bug), When i run the same app on my emulator it works fine and if i disconnect my internet the onErrorResponse is also working ,but it crashes when i run it on other phones

private void getTeacherData() {

    if (username.equals("")) {
        Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
        return;
    }
        String url = DATA_URL+username;

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }
                });

       RequestQueue requestQueue = Volley.newRequestQueue(this);
       requestQueue.add(stringRequest);
}

private void showJSON(String response){
    try {
        JSONObject jsonObject =new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
        for(int x=0;x<result.length();x++) {

            JSONObject collegeData = result.getJSONObject(x);
            albumList.add(new Album(collegeData.getString(config.course_name),collegeData.getString(config.semester)
                    ,collegeData.getString(config.section),collegeData.getString(config.total_classes)
                    ,collegeData.getString(config.subject),collegeData.getString(config.classes_taken),collegeData.getString(config.subject_code)));
            adapter.notifyDataSetChanged();
        }
    } catch (JSONException e) {

        e.printStackTrace();
    }

}

things i have tried :

  1. Removed showJSON(); method inside onResponse().
  2. Surrounded the string request with try catch.
  3. Surrounded whole getTeacherData() with try catch.
1

There are 1 best solutions below

0
Sarmad Shah On

It seems like onErrorResponse responds differently on few devices

onErrorResponse returned null on few devices (that was the reason for the crash)

      new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                        if(error.getMessage==NULL){
      Toast.makeText(cardview.this, "Failed to retrieve data", Toast.LENGTH_LONG).show();
                        }
                    else{
                            Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                        }

                    });