【问题标题】:W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObjectW/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray 无法转换为 JSONObject
【发布时间】:2020-06-07 10:58:12
【问题描述】:
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)

我的错误

我想从 android 中的数据库中获取记录。为此,我尝试使用此代码从数据库中获取记录。我使用了 volley 库,它工作正常,错误是转换 W 的 at value[] 0 /System.err: org.json.JSONException: org.json.JSONArray 类型为 0 的值 [] 无法转换为 JSONObject W/System.err:在 org.json.JSON.typeMismatch(JSON.java:101) 我已经完全提到了这一点。

【问题讨论】:

  • 你能给我们看一个 JSON 响应的样本吗?
  • {"users":[[],{"0":"1","id":"1","1":"","username":"","2 ":"","email":"","3":"","password":""},{"0":"2","id":"2","1":"" ,"用户名":"","2":"","email":"","3":"","密码":""},{"0":"3","id": "3","1":"","用户名":"","2":"","email":"","3":"","密码":""},{"0 ":"4","id":"4","1":"ABC","用户名":"ABC","2":"abc@gmail.com","email":"abc@gmail .com","3":"97532","password":"97532"},{"0":"5","id":"5","1":"ABC","用户名": "ABC","2":"abx@gmail.com","email":"abx@gmail.com","3":"123345","password":"123345"}]}
  • 这是 Json 响应示例

标签: java android android-studio


【解决方案1】:

api 返回的 JSON 响应存在问题 https://androidwork2132.000webhostapp.com/fetch.php

JSON 响应中 users 数组的第一项是一个数组,它必须是一个对象,就像其他对象一样

    {"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"}]}

【讨论】:

  • 如何解决这个问题
  • 通过您的服务器从 API 响应中删除 [],
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 2014-03-20
  • 1970-01-01
相关资源
最近更新 更多