带有JSON的webservice

时间:2018-05-30 08:21:32

标签: java android sql json

我正在尝试创建一个从Web服务获取JSON数据的应用程序并将其放在listview中,但这不起作用。它没有崩溃或给出错误消息。网络服务没有给我任何数据。

这是我的代码:公共类MainActivity扩展了AppCompatActivity {

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

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

    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 adres";
        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 ja = new JSONArray(dbResult);
        JSONObject jo = null;
        data = new String[ja.length()];
        for (int i = 0; i < ja.length(); i++){
            jo = ja.getJSONObject(i);
            data[i] = jo.getString("Naam");

        }
        adapter = new ArrayAdapter<String>(this, R.layout.layout_list, R.id.list_item, data);
        listview.setAdapter(adapter);
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

}

这是来自Web服务的JSON数据:

orders: [
{
naam: "J. Hecker",
adres: "St Rochusstraat 40",
postcode: "5611 RJ",
bestelling: [
          {
            Pizza: "Siciliane"
          },
          {
            Pizza: "4-Stagione"
          },
          {
            Pizza: "Hawaii"
          },
          {
            Pizza: "Siciliane"
          }
        ]
},

这是Logcat:

05-30 11:22:26.415 11361-11361/com.example.stefan.mad_oefenexamen W/System.err: org.json.JSONException: Value {"orders":[{"naam":"P. Melissen","adres":"Grote Berg 101","postcode":"5611 KJ","bestelling":[{"Pizza":"Hawaii"},{"Pizza":"Siciliane"},{"Pizza":"Napolitane"},{"Pizza":"Margarita"},{"Pizza":"Margarita"}]},{"naam":"M.  Dijk","adres":"Jan Smitzlaan 2","postcode":"5611 LE","bestelling":[{"Pizza":"Margarita"}]},{"naam":"Th.M. Neessen","adres":"Hoefkestraat 31","postcode":"5611 RM","bestelling":[{"Pizza":"Napolitane"}]},{"naam":"T. Bazelmans","adres":"Hertog Hendrik van Brabantplein 29","postcode":"5611 PG","bestelling":[{"Pizza":"4-Stagione"},{"Pizza":"Hawaii"},{"Pizza":"Siciliane"},{"Pizza":"Margarita"},{"Pizza":"Salami"},{"Pizza":"4-Stagione"}]},{"naam":"L. Cals","adres":"Geldropseweg 5","postcode":"5611 SB","bestelling":[{"Pizza":"Margarita"},{"Pizza":"Napolitane"}]}]} of type org.json.JSONObject cannot be converted to JSONArray

1 个答案:

答案 0 :(得分:0)

问题是来自服务器的响应是JSONObject而不是JSONArray。此JSONObject包含以下

的JSONArray
{
naam: "J. Hecker",
adres: "St Rochusstraat 40",
postcode: "5611 RJ",
bestelling: [
      {
        Pizza: "Siciliane"
      },
      {
        Pizza: "4-Stagione"
      },
      {
        Pizza: "Hawaii"
      },
      {
        Pizza: "Siciliane"
      }
    ]
    },

要获取这些值,请假设您的JSONObject响应是jsonResponseObject

try{
 JSONArray jsonArray=jsonResponseObject.getJSONArray("orders");
 for(int i=0;i<jsonArray.length(),i++){
 JSONObject jsonObject=jsonArray.getJSONObject(i);
 Log.d("Naam",jsonObject.getString("naam"));
 }catch(Exception e){
    e.printstacktrace();
 }

}