#FATAL EXCEPTION while converting json string to json array or json object

38 messages · Page 1 of 1 (latest)

vital python
#

I'm getting below String:

[{"name":"abc", "roll": "123"}]

By the below code line:

response.body().string()

How can we convert it to JSON array that I can loop through each object and can access each property?
If I put that string to any variable like below;

String res = response.body().string();

It shows below FATAL EXCEPTION:

FATAL EXCEPTION: OkHttp Dispatcher

If it would okay then I would go with below lines:

JSONArray jarr = new JSONArray(res);
Log.e("DAILY", "" + jarr.length());

Any hints, what is going on?
Thanks!

thick bloomBOT
#

This post has been reserved for your question.

Hey @vital python! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

slow flower
vital python
#

Yes response.body().string() give out [{"name":"abc", "roll": "123"}] I told it

slow flower
#

Then why did you tell it shows FATAL EXCEPTION: OkHttp Dispatcher?

vital python
#

Because after assigning that output to another String variable I see FATAL EXCEPTION in log cat

slow flower
#

I'm not knowledgeable about OkHttp, but have you tried using the first variable rather than calling response.body().string() again to assign it to another variable?

vital python
#

Have a look at screenshot

#

Forget about okHttp, why don't we convert the string to JSON Array or JSON Object?

slow flower
#

What do you mean?

vital python
#

Mean this is a String [{"name":"abc", "roll": "123"}] But it shows it is a JSON String.
Then we can convert it to JSON Array or JSON Object then I can access the data properly

slow flower
#

Well, don't ask others why you aren't doing that. It looks like you should be doing that indeed

vital python
#

What do you mean?

slow flower
#

You're asking why haven't you converted the string to JSONArray.
Don't ask people why you didn't do that. Do that instead

vital python
#

If I do not convert that string to JSON Array or JSON Object then how can I access my desire content from it?

slow flower
#

That reading comprehension problem, and that written expression problems you have, are they usual, or is it just because you're not really focusing on the task?

#

I'm telling you that yes, you need to convert your string to JSONArray

#

So far you're not doing that, so yes, the choice to do that instead is the better thing to do

vital python
#

My ask is how can I convert the string to JSONArray, I tried many ways

#
myResponse = new JSONArray(response.body());
JSONArray finalMyResponse = myResponse;
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("DAILY","" + finalMyResponse.length());
                            for (int i = 0; i < finalMyResponse.length(); i++) {
                                // Assuming your JSONArray contains JSONObjects
                                try {
                                    JSONObject entry = finalMyResponse.getJSONObject(i);
                                    Log.e("DAILY","" + finalMyResponse.getJSONObject(0));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            // Or simply in your case:
//                            mTextView.setText(myResponse.getJSONObject(0));
                        }
                    });

Just tried this but no luck!

slow flower
#

What's it doing instead?

vital python
#

If I see the below line's output:

Log.e("DAI", String.valueOf(response.body().string().length()));

It shows me FATAL EXCEPTION, very strange!
Can't I see the string length now 😭

slow flower
#

I think that forgetting about OkHttp was stupid. Come back when you don't want to do that anymire

vital python
#

What should I try now instead of OkHttp?

slow flower
#

........

#

You didn't answer me about your reading comprehension problems. Are they normal?

vital python
#

Which one I did not answer?

#

java.lang.IllegalStateException: closed
Showing this error for the line:

Log.e("DAI", String.valueOf(response.body().string().length()));
slow flower
#

I asked whether your reading comprehension problems are normal

vital python
#

Can't understand properly

slow flower
#

You can't do this because you're calling response.body().string() more than once.
A solution would be to call it no more than once.
So, calling it a negative number of times, or zero times, or once.
I don't think calling it a negative number of times is possible.
I think if you call it zero times then you won't have access to the response.
But you could probably call it once. That should work.
Remember, no more, than once.

vital python
#

Ok then after getting response I can save it to a String variable one, then I can use it, right?

slow flower
#

right

vital python
#

Hmm my Log.e is now working, let me parse it now.
Hope now I can parse the string to JSON Array

#

Thanks @slow flower it is working now with JSON Array

thick bloomBOT