1

I have this piece of code:

HJRestClient.post(path, params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {
                JSONArray array = response.getJSONArray("tags");
                if (adDetailViewAdapter != null) {
                    adDetailViewAdapter.setTags(array);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });

It is crashing on the line JSONArray array = response.getJSONArray("tags"); with exception W/System.err: org.json.JSONException: No value for tags

While I have written code for catching the exception, it's not getting caught and causing the app to crash.

I have 2 devices Samsung Galaxy Note 5 with Android v6.0.1 in which the exception is caught successfully.

Another Huawei Mate 7 with Android v4.4.2 in which the exception is not getting caught and causing a crash.

I also wrote:

catch (Exception exception) {}

but still it's getting crashed.

Any help?

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65

2 Answers2

0

You should this way:

HJRestClient.post(path, params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            try {
                if(response!=null&&response.has("tags")){
                    JSONArray array = response.getJSONArray("tags");
                    if (adDetailViewAdapter != null) {
                        adDetailViewAdapter.setTags(array);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • I wrote your code and now its crashing but without showing the exception type. – Abdullah Umer May 10 '16 at 11:45
  • @AbdullahUmer, Edited my answer, please check – Hiren Patel May 10 '16 at 11:47
  • Hey, thanks for your help. It turned out there was a bug in code for Grid adapter where the tags were used. Android studio was not showing the correct exception but Crashlytics showed Fatal Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams at android.widget.GridView.onMeasure(GridView.java:1046) – Abdullah Umer May 10 '16 at 12:08
  • @AbdullahUmer, Glad to help you. – Hiren Patel May 10 '16 at 12:09
0

I have the same situation, even if I wrapped the json parse method inside try catch, it was never catching the exception. So I explicitly added

 catch (e: Exception) {
        when (e) {
            is SomeExceptionType -> {
                throw MyException()
            }
            is JSONException -> {
                throw BackendErrorException("Something is coming wrong from the server!")
            }
            else -> throw e
        }
    }
Azamat Mahkamov
  • 912
  • 14
  • 18