java.lang.Integer 类型的值无法转换为 JSONObject

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

我想显示 我的 android 来自 JSON 的高分。但我有错误说 value 100 at HS of type java.lang.Integer cannot be converted to JSONObject

这是 JSON 的样子

{
"HS": 100
}

这是我的代码,用于显示 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();
                    }
                });

    }

你需要更换

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

int hs = response.getInt("HS")

因为response已经是JSONObject

如果你的json是这样的

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

那你可以

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

不要忘记在 getData() textView tx_hscore 声明之后写下

并在您的 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();
                    }
                });

    }