0

I want to display a high score for my android from JSON. But I have error that said value 100 at HS of type java.lang.Integer cannot be converted to JSONObject

here is the JSON looks like

{
"HS": 100
}

here is my code to show the value of the JSON

public void getData(){
        AndroidNetworking.get("http://192.168.1.19/web_admin/public/api/highSkor_api")
                .setPriority(Priority.LOW)
                .build()
                .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                            //adding the product to product list
                            try {
                                //get JSON data
                                JSONObject data = response.getJSONObject("HS");
                                int HS = data.getInt("HS");
                                tx_hscore.setText(getString(R.string.high_score, HS);
                                Log.d(TAG, "ISI : "+HS);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        //Toast.makeText(Result.this, "High Score : "+data_highSkor.getHighSkor(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(ANError error) {
                        Log.e(TAG, "onError : "+ error);
                        Toast.makeText(Result.this, "Failed to reach server : "+error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

    }
2
  • I tried to parse the JSON as response.getInt("HS") and for me it works fine. Are you sure that the error is at the same line. Can you post your stack trace. Commented Dec 14, 2020 at 13:36
  • @akhilnair whoops! sorry, I put the wrong code in my question.. I've edit it, this edited code should be my right code for my question. Sorry, 'cause I've just change it before write my question... By the way, if I use that code, my application would crash and get back to another layout. Commented Dec 14, 2020 at 16:22

2 Answers 2

1

You need to replace

JSONObject data = response.getJSONObject("HS");

with

int hs = response.getInt("HS")

because response is already a JSONObject

if your json was like this

{
"HS": {
     "key1": "value1",
     "key2": "value2"
    }
 }

Then you could do

JSONObject data = response.getJSONObject("HS");

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answering @akhilnair!.. I was trying to replace it like that, but why is my application crashed? everytime I'm trying to getInt from response directly, my application just crashed. It will go back to the previous page.
Can you please tell what the error you get?
I can't see the error. But in the locgat shows error like pid(and_some_numbers). That's what I remember. I'm trying to open my android studio right now, but my ram is just 4gb so, it still not responding :'') sorry.
I think its a different issue, but let me know the error when your studio is back.
my studio is back!.. I was trying to code like your answer (that what I code before I try the code in my question), and it works!! This is my bad, sorry. I just relized that I was write the getData() above the textView declaration. That's why my application was crashed. and when I try to code it again, and move the getData() after the textView declaration, it was succesfully runnning without any error :)
|
0

don't forget to write your getData() after the textView tx_hscore declaration.

and use this code in your getData() :

public void getData(){
        AndroidNetworking.get("http://192.168.1.19/web_admin/public/api/highSkor_api")
                .setPriority(Priority.LOW)
                .build()
                .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                            //adding the product to product list
                            try {
                                //get JSON data                                
                                int HS = response.getInt("HS");
                                tx_hscore.setText(getString(R.string.high_score, HS);
                                Log.d(TAG, "ISI : "+HS);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        //Toast.makeText(Result.this, "High Score : "+data_highSkor.getHighSkor(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(ANError error) {
                        Log.e(TAG, "onError : "+ error);
                        Toast.makeText(Result.this, "Failed to reach server : "+error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.