Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject

Posted on November 11, 2022 By

Solution 1 :

There is an issue with the JSON reponse returned by the api
https://androidwork2132.000webhostapp.com/fetch.php

The first item in users array in JSON response is an array, it must be an object like rest of the objects

    {"users":[[], // This array here, it should be an object like rest of the objects.
    {"0":"1","id":"1","1":"","username":"","2":"","email":"","3":"","password":""},
{"0":"2","id":"2","1":"","username":"","2":"","email":"","3":"","password":""},
{"0":"3","id":"3","1":"","username":"","2":"","email":"","3":"","password":""},
{"0":"4","id":"4","1":"ABC ","username":"ABC ","2":"[email protected]","email":"[email protected]","3":"97532","password":"97532"},
{"0":"5","id":"5","1":"ABC ","username":"ABC ",
"2":"[email protected]","email":"[email protected]","3":"123345","password":"123345"}]}

Problem :

package com.example.signup2;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    ListView listView;
    ArrayList<UserData> arrayList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        listView = findViewById(R.id.listview);
        fetchData();
    }

    public void fetchData()
    {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://androidwork2132.000webhostapp.com/fetch.php", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("users");

                    for (int i = 0;i < jsonArray.length(); i++)
                    {
                        UserData userData = new UserData(jsonArray.getJSONObject(i).getString("id"),jsonArray.getJSONObject(i).getString("username"),
                                jsonArray.getJSONObject(i).getString("email"),jsonArray.getJSONObject(i).getString("password"));
                        arrayList.add(userData);
                    }
                    CustomAdapter customAdapter = new CustomAdapter();
                    listView.setAdapter(customAdapter);
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(Main2Activity.this,error.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(Main2Activity.this);
        requestQueue.add(stringRequest);
    }

    class CustomAdapter extends BaseAdapter
    {

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = getLayoutInflater().inflate(R.layout.listviewitem,null);
            TextView id,username,email,password;
            id = findViewById(R.id.id);
            username = findViewById(R.id.username);
            email = findViewById(R.id.email);
            password =findViewById(R.id.password);

            id.setText(arrayList.get(position).getId());
            username.setText(arrayList.get(position).getUsername());
            email.setText(arrayList.get(position).getEmail());
            password.setText(arrayList.get(position).getPassword());

            return view;
        }
    }
}

MainActivity.java

W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONArray.getJSONObject(JSONArray.java:525)
        at com.example.signup2.Main2Activity$1.onResponse(Main2Activity.java:54)
        at com.example.signup2.Main2Activity$1.onResponse(Main2Activity.java:43)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:226)
        at android.app.ActivityThread.main(ActivityThread.java:7208)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)

My error

READ  [FIXED] java - Increment a variable whenever onCreate() is called
Powered by Inline Related Posts

I want to fetch the records from database in android.For this purpose i have tried this code for fetching the records from database.I have used volley library which is working fine the error is converting the at value[] 0 of W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject
W/System.err: at org.json.JSON.typeMismatch(JSON.java:101)
i have mentioned this completely.

Comments

Comment posted by libanbn

Can you show us a sample of the JSON response?

Comment posted by Hammad

{“users”:[[],{“0″:”1″,”id”:”1″,”1″:””,”username”:””,”2″:””,”email”:””,”3″:””,”password”:””},{“0″:”2″,”id”:”2″,”1″:””,”username”:””,”2″:””,”email”:””,”3″:””,”password”:””},{“0″:”3″,”id”:”3″,”1″:””,”username”:””,”2″:””,”email”:””,”3″:””,”password”:””},{“0″:”4″,”id”:”4″,”1″:”ABC “,”username”:”ABC “,”2″:”[email protected]”,”email”:”[email protected]”,”3″:”97532″,”password”:”97532″},{“0″:”5″,”id”:”5″,”1″:”ABC “,”username”:”ABC “,”2″:”[email protected]”,”email”:”[email protected]”,”3″:”123345″,”password”:”123345″}]}

Comment posted by Hammad

This is sample of Json response

Comment posted by Hammad

How to fix this

Comment posted by Alpha 1

Remove

Android Tags:android, android-studio, java

Post navigation

Previous Post: [FIXED] java – How to handle large amount of animated vector drawables?
Next Post: [FIXED] c# – Call a self hosted web api from an Android app through a DDNS

Related Posts

[FIXED] android – DateTime in Xaxis Label not working correctly MPAndroidChart Android
[FIXED] kotlin – Will a QR-code in an Android Automotive app be approved? Android
[FIXED] Firebase + android method for generating a unique token for each user of my application independent from the device? Android
[FIXED] android – Repeating background service using Alarm Manager Android
[FIXED] firebase – com.google.android.gms:play-services-ads | ERROR Android
[FIXED] android – Flutter apk builds, installs but then will not open Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme