stefan de boer
stefan de boer

Reputation: 405

JSON parsing to listview/arrayadapter

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.

This is my code :

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();
    }
}
  }

This is the error in the logcat :

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

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

Upvotes: 0

Views: 105

Answers (2)

sunil kumr
sunil kumr

Reputation: 26

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.

Upvotes: 1

Krzysztof Kubicki
Krzysztof Kubicki

Reputation: 676

You're getting a JSONObject from db, not an array, the array is inside this object. You should map the response from web service to JSONObject and the map the bestelling field to JSONArray. And.. as was mentioned in the comment use some lib to help you with that, like GSON.

Upvotes: 1

Related Questions