简体   繁体   English

W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray 无法转换为 JSONObject

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

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 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.我想从 android 中的数据库中获取记录。为此,我尝试使用此代码从数据库中获取记录。我使用了 volley 库,它工作正常,错误是转换 W/System 的 at value[] 0。 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完全地。

There is an issue with the JSON reponse returned by the api https://androidwork2132.000webhostapp.com/fetch.php 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 JSON 响应中用户数组中的第一项是一个数组,它必须是一个 object,如对象的 rest

    {"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":"abc@gmail.com","email":"abc@gmail.com","3":"97532","password":"97532"},
{"0":"5","id":"5","1":"ABC ","username":"ABC ",
"2":"abx@gmail.com","email":"abx@gmail.com","3":"123345","password":"123345"}]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.json.JSONException:值<jsonarray-here> org.json.JSONArray 类型无法转换为 JSONObject</jsonarray-here> - org.json.JSONException: Value <JSONarray-here> of type org.json.JSONArray cannot be converted to JSONObject W / System.err:org.json.JSONException:值 - W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject W/System.err: org.json.JSONException: 类型 java.lang.String 的值数组无法转换为 JSONObject - W/System.err: org.json.JSONException: Value array of type java.lang.String cannot be converted to JSONObject W/System.err: org.json.JSONException: 类型 java.lang.String 的值数据库无法转换为 JSONObject - W/System.err: org.json.JSONException: Value Database of type java.lang.String cannot be converted to JSONObject Volley库W / System.err:org.json.JSONException:类型为java.lang.String的Value Connected无法转换为JSONObject - Volley library W/System.err: org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject W / System.err:org.json.JSONException:值 - W/System.err: org.json.JSONException: Value <!— of type java.lang.String cannot be converted to JSONObject 获取错误:解析数据 org.json.JSONException 时出错:无法将 org.json.JSONArray 类型的值转换为 JSONObject - getting error :Error parsing data org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject JSON解析器:解析数据org.json.JSONException时出错:类型org.json.JSONArray无法转换 - JSON Parser: Error parsing data org.json.JSONException: type org.json.JSONArray cannot be converted Android 中的错误'JSONException: org.json.JSONArray 类型的值 [] 无法转换为 JSONObject' - Error 'JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject' in Android 无法在org.json.JSONArray类型的0值[]转换为android中的JSONObject? - Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM