Я получил данные JSON из URL с помощью библиотеки ION, но у меня возникла проблема с отображением их в listView в Android Studio.
Я отлаживаю их следующим образом:
JSONObject jsonObject = new JSONObject(String.valueOf(result));
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
и вот выходные данные для отладки:
result = {JsonObject@5328} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"
result = {JsonObject@5328} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"
jsonObject = {JSONObject@5341} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"
id
- это целочисленная форма, отправленная с первого действия. Код моей активности приведен ниже:
public class ContactsActivity extends AppCompatActivity {
ListView contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Intent intent = getIntent();
contactList = findViewById(R.id.contactList);
int dId = intent.getExtras().getInt("id");
String Sid = new String(String.valueOf(dId));
Context context = getApplicationContext();
Toast.makeText(context, "" + dId, Toast.LENGTH_SHORT).show();
try {
getContacts(Sid);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getContacts(final String Sid) throws Exception {
class Contacts{
String empName;
String designation;
String mobile;
String landlineOffice;
String landlineRes;
String fax;
String email;
public Contacts(String designation, String mobile, String landlineOffice, String landlineRes, String fax, String email) {
this.designation = designation;
this.mobile = mobile;
this.landlineOffice = landlineOffice;
this.landlineRes = landlineRes;
this.fax = fax;
this.email = email;
}
}
Ion.with(this)
.load("http://10.180.243.19:8080/api/departments/" + Sid)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
int id = Integer.parseInt(Sid);
try {
JSONObject jsonObject = new JSONObject(String.valueOf(result));
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
});
}
}
Я ожидаюотобразить список контактных данных нескольких сотрудников в виде списка.