Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

java - 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"
            }
         ]
      }
   ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...