-1

I keep on getting this error. I get the error JSONArray cannot be converted to JSONObject. I have parsed the JSON file which contains array of objects, but still getting this error. Here is the code:

JAVA Code:

public class DetailActivity extends AppCompatActivity {

ImageView imageView;
TextView options, bio, noBio;
private final String JSON_URL = "https://run.mocky.io/v3/987b97bd-6895-40d1-9d37-ce404162c491";
private JsonObjectRequest jsonObjectRequest;
private RequestQueue requestQueue;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    getSupportActionBar().setTitle("");
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    imageView = (ImageView) findViewById(R.id.iv_image);
    options = (TextView) findViewById(R.id.tv_options);
    bio = (TextView) findViewById(R.id.tv_bio);
    noBio = (TextView) findViewById(R.id.tv_no_bio);

    getDetails();
}

private void getDetails() {
    jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL,null ,new Response.Listener<JSONObject>() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = new JSONArray(response.toString());
                for (int i = 0; i<jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String type = jsonObject.getString("type");
                    boolean isVisible = jsonObject.getBoolean("isVisible");

                    if (!isVisible){
                        imageView.setVisibility(View.GONE);
                        options.setText("API response is false. So data will not be shown.");
                        bio.setText("API response is false. So data will not be shown.");
                    }
                    else{
                        imageView.setVisibility(View.VISIBLE);
                        options.setVisibility(View.VISIBLE);
                        bio.setVisibility(View.VISIBLE);

                        Intent intent = getIntent();
                        Glide.with(getApplicationContext()).load((Bitmap) intent.getParcelableExtra("image")).into(imageView);
                        options.setText("Choosen Option : " + getIntent().getStringExtra("options"));
                        String str_bio = getIntent().getStringExtra("comments");
                        if (str_bio == null){
                            bio.setVisibility(View.GONE);
                            noBio.setVisibility(View.VISIBLE);
                        }
                        else{
                            bio.setText("Bio : " + str_bio);
                            noBio.setVisibility(View.GONE);
                        }
                    }
                }
            } catch (JSONException e) {
                Toast.makeText(DetailActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d("Exception", e.getMessage());
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(DetailActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d("Error", error.getMessage());
        }
    });
    requestQueue = Volley.newRequestQueue(DetailActivity.this);
    requestQueue.add(jsonObjectRequest);
}

}

XML File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0E6FD"
android:orientation="vertical"
tools:context=".DetailActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp">
    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"/>
    <TextView
        android:id="@+id/tv_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choosen option : "
        android:textSize="17dp"
        android:fontFamily="@font/notoserif_bold"
        android:layout_marginTop="15dp"
        android:textColor="@color/black"/>
    <TextView
        android:id="@+id/tv_bio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bio :"
        android:textSize="17dp"
        android:fontFamily="@font/notoserif_bold"
        android:layout_marginTop="15dp"
        android:textColor="@color/black"/>
    <TextView
        android:id="@+id/tv_no_bio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No Bio provided"
        android:gravity="center"
        android:textSize="15dp"
        android:fontFamily="@font/notoserif_regular"
        android:layout_marginTop="15dp"
        android:textColor="@color/black"/>
</LinearLayout>

Here is the JSON File:

[
   {
      "type":"PHOTO",
      "isVisible":true
   },
   {
      "type":"SINGLE_CHOICE",
      "isVisible":true
   },
   {
      "type":"COMMENT",
      "isVisible":true
   }
]
9
  • can you post the logcat tht indicates the error? Commented Nov 28, 2020 at 4:18
  • there is no error being shown in logcat. i only get volley error which i have added above. Commented Nov 28, 2020 at 4:22
  • 1
    what particular line?. on your post , you have a log Log.d("Error", error.getMessage()); Commented Nov 28, 2020 at 4:25
  • 1
    also make you catch like this catch (Throwable e) and print it like this e.printStackTrace() Commented Nov 28, 2020 at 4:26
  • 2
    You are making a JsonObjectRequest but the response is a json array use JsonArrayRequest Commented Nov 28, 2020 at 4:42

1 Answer 1

0

write code like this for extracting JSON:

Java file code after getting response from server:

     String json_string = "[ {type:PHOTO, isVisible:true }, { type:SINGLE_CHOICE, isVisible:true }, { type:COMMENT, isVisible:true } ]";

        try {
            JSONArray jsonArray = new JSONArray(json_string);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                String type = obj.getString("type");
                boolean isVisible = obj.getBoolean("isVisible");

                Log.e("json", "type ::" + type);
                Log.e("json", "isVisible ::" + isVisible);
            }


        } catch (JSONException e) {

            Log.e("Json error", "onCreate:" + e.getMessage());
        }

Result :

2020-11-28 11:50:08.806 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::PHOTO
2020-11-28 11:50:08.806 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::SINGLE_CHOICE
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::COMMENT
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.