在android studio中显示JSON typeMismatch错误

时间:2017-07-09 05:40:00

标签: java android json

我想从服务器获取数据库中的数据....但是我得到了JSON.type不匹配错误............这里是我的代码...... ....任何帮助将不胜感激.. .............

public class MainActivity extends AppCompatActivity {

private static final String URL_DATA = "https://........php";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
String jsonData;

private List<ListItem> listitems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    listitems = new ArrayList<>();

    loadRecyclerViewData();
}

    private void loadRecyclerViewData()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading data...");
        progressDialog.show();

        StringRequest  stringRequest = new StringRequest(Request.Method.GET,
                URL_DATA,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String s)
                    {
                        progressDialog.dismiss();
                        try {
                               JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray(jsonData);

                            for(int i=0;i<array.length();i++) {
                                JSONObject o = array.getJSONObject(i);
                                ListItem item = new ListItem(
                                        o.getString("Name"),
                                        o.getString("Address")
                                );

                                listitems.add(item);
                            }
                            adapter = new MyAdapter(listitems,getApplicationContext());
                            recyclerView.setAdapter(adapter);
                        }
                        catch(JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), volleyError.getMessage(),Toast.LENGTH_LONG).show();
                    }

                    });

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

    }
}

这是logcat

 07-09 10:22:10.735 25053-25053/sensor.recyclercardview2 W/System.err:     
 at org.json.JSON.typeMismatch(JSON.java:111)
 07-09 10:22:10.735 25053-25053/sensor.recyclercardview2 W/System.err:     
 at org.json.JSONObject.<init>(JSONObject.java:160)
 07-09 10:22:10.735 25053-25053/sensor.recyclercardview2 W/System.err:     
 at org.json.JSONObject.<init>(JSONObject.java:173)
 07-09 10:22:10.735 25053-25053/sensor.recyclercardview2 W/System.err:          
 at sensor.recyclercardview2.MainActivity$1.onResponse(MainActivity.java:63)
 07-09 10:22:10.735 25053-25053/sensor.recyclercardview2 W/System.err:     
 at sensor.recyclercardview2.MainActivity$1.onResponse(MainActivity.java:57)

1 个答案:

答案 0 :(得分:0)

替换这两行

JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray(jsonData);

用这个

JSONArray array = new JSONArray(s);