0

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"
            }
         ]
      }
   ]
}
4
  • simple use the google GSON ..google it Commented May 30, 2018 at 10:20
  • Also try to log the result of your request. its possible that your HTTP request is not returning json Commented May 30, 2018 at 10:21
  • Hi @Stefan first you have to convert your result to JSON object since it is json object and loop through inside orders array Commented May 30, 2018 at 10:23
  • hey @Shanmugam how can i do that with my code? Commented May 30, 2018 at 10:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

it is not working. It is giving an error on i at jo = arr.getJSONObject(i) This is the error message : .getJSONObject (java.lang.String) in JSONObject cannot be applied to (int)
I also updated the full JSON data from the web service.
JSONObject jsonobject = new JSONObject(dbResult); JSONArray jsonarray = jsonobject.getJsonArray("orders"); To find the length of jsonarray use; int jsonarrayLength = jsonarray.length(); // This will count the number of objects in the array. Here you are working on the wrong approach while accessing the "name"; don't use any loop here.
1

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.

4 Comments

Would you mind doing that for me? I have no idea how to do that.
If you do not have an idea, get an idea. It's easy and more importantly, now you know what to do. Asking someone for doing smth that you know what to do is very rude.
he could've just said 'no I don't want to' that would've been fine too. not responding is rude as well. Plus I'm in a hurry so I don't have the time to spend 2 hours on google trying to find the answer.
@stefandeboer: milano is correct (though I might have phrased it in more diplomatic terms). We encourage self-reliance here, and the ratio of helpers to people wanting help means we're really wary of succumbing to free work requests. Can you use the new information you have been given as a jumping-off point for new research? It may be simpler to do than you think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.