JSON解析到listview/arrayadapter [英] JSON parsing to listview/arrayadapter

查看:93
本文介绍了JSON解析到listview/arrayadapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个将JSON数据解析为列表视图的应用程序.这对我不起作用;它没有给我任何数据.

I am making an app that parses JSON data to a listview. This is not working for me; it is not giving me any data back.

这是我的代码:

public class MainActivity extends AppCompatActivity {

ListView listview;
ArrayAdapter<String> adapter;
String[] data;

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

    listview = (ListView)findViewById(R.id.listview);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    MPWebservice();

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, bestelling.class);
            intent.putExtra("naam", listview.getItemAtPosition(position).toString());
            startActivity(intent);
        }
    });

}


private void MPWebservice() {
    String Webadres = null;
    String dbResult = "empty";
    dbConnect database = new dbConnect(this);

    try {
        String query = "SELECT * FROM orders";
        Webadres = "?query=" + URLEncoder.encode(query, "UTF-8");
        String con = "https://amje.000webhostapp.com/mariosPizzaJSON.php" + Webadres;
        dbResult = database.execute(con).get();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        JSONArray arr = new JSONArray(dbResult);
        JSONObject jo = null;
        data = new String[arr.length()];

        for (int i = 0; i < 1; i++) {
            jo = arr.getJSONObject(i);
            data[i] = jo.getString("name");
        }
        adapter = new ArrayAdapter<String>(this, R.layout.layout_list, R.id.list_item, data);
        listview.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  }

这是logcat中的错误:

This is the error in the logcat :

of type org.json.JSONObject cannot be converted to JSONArray

这是网络服务中的json:

This is the json in the webservice :

{
   "orders":[
      {
         "naam":"J. Peeters",
         "adres":"Kettingstraat 12",
         "postcode":"5611 RD",
         "bestelling":[
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"Margarita"
            }
         ]
      },
      {
         "naam":"H. Wissink",
         "adres":"Frederik van Eedenplein 5",
         "postcode":"5611 KT",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Siciliane"
            },
            {
               "Pizza":"4-Stagione"
            }
         ]
      },
      {
         "naam":"M. Huisman",
         "adres":"Hertogstraat 17",
         "postcode":"5611 PB",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Siciliane"
            },
            {
               "Pizza":"Salami"
            }
         ]
      },
      {
         "naam":"H. Moors",
         "adres":"Mauritsstraat 9",
         "postcode":"5611 GV",
         "bestelling":[
            {
               "Pizza":"Calzone"
            }
         ]
      },
      {
         "naam":"H. Jansen",
         "adres":"Stationsplein 23",
         "postcode":"5611 AC",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"4-Stagione"
            }
         ]
      },
      {
         "naam":"G.M. Verkuijlen-vd Ven",
         "adres":"Tramstraat 54",
         "postcode":"5611 CR",
         "bestelling":[
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"Margarita"
            }
         ]
      }
   ]
}

推荐答案

问题在这里JSONArray arr = new JSONArray(dbResult); 将其更改为JSONObject arr = new JSONObject(dbResult); 似乎您尚未发布完整的json数据.如果仍然无法解决,请发布完整的json data.

The problem is here JSONArray arr = new JSONArray(dbResult); Change it to JSONObject arr = new JSONObject(dbResult); seems that you have not posted the complete json data. If it doesn't solve please post complete json data.

这篇关于JSON解析到listview/arrayadapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆