the json looks like this:
[{"id":"1","name":"Mihai","email":"[email protected]","password":"1234","phone":"765889345"},{"id":"2","name":"Robin","email":"[email protected]","password":"1234","phone":"765453434"}]
the code
// Json
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
.penaltyLog()
.build());
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
TextView uid = (TextView) findViewById(R.id.titlu_anunt1);
TextView name1 = (TextView) findViewById(R.id.descriere_anunt1);
TextView email1 = (TextView) findViewById(R.id.telefon_anunt1);
JSONObject json = null;
String str = "";
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://appz.esy.es/get_user.php");
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
try {
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0);
uid.setText(json.getString("id"));
name1.setText(json.getString("name"));
email1.setText(json.getString("email"));
}
catch (JSONException e) {
e.printStackTrace();
}
How can I add all the objects to my textviews? Right now I can only display one object from json in my first textview, I want to increment the id. I want to display the id1 information: name, location and other stuff in textviews 1. After that I want to display for id2 the name and location into textviews 2.
You can use the volley library, it's easy to use and you can focus on your app rather than on how getting your data. You can add the library to your project following this tutorial or if you are using AndroidStudio you can add it to your gradle as seen here.
To make a request you just need to add this: