stupirl
stupirl

Reputation: 9

value of type java.lang.Integer cannot be converted to JSONObject

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();
                    }
                });

    }

Upvotes: 0

Views: 1093

Answers (2)

stupirl
stupirl

Reputation: 9

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();
                    }
                });

    }

Upvotes: 0

akhil nair
akhil nair

Reputation: 1551

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");

Upvotes: 1

Related Questions