0
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

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.

3
  • Can you show us a sample of the JSON response? Commented Jun 7, 2020 at 11:04
  • {"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"}]} Commented Jun 7, 2020 at 12:27
  • This is sample of Json response Commented Jun 7, 2020 at 12:27

1 Answer 1

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"}]}
Sign up to request clarification or add additional context in comments.

2 Comments

How to fix this
Remove [], from the API response through your server.

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.