Reputation: 49
In my android application I'm receiving an JSONArray now I should parse that and should show the data in the listview. I tried it and it is showing '{}' in all list items in listview.Cannot understand why its showing like that.
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("topicsJSON",composeJSON());
client.post("http://www.example.com/LSM_Sci-Mat/load_topics.php",params,new AsyncHttpResponseHandler()
{
public void onSuccess(String response)
{
Gson gson = new GsonBuilder().create();
try
{
JSONArray arr = new JSONArray(response);
for (int i = 0; i < arr.length(); i++)
{
JSONObject jsonObject = new JSONObject();
list.add(jsonObject.toString());
load_data();
//Toast.makeText(getApplicationContext(), "Element ==> "+list, 5000).show();
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Throwable error,String content)
{
if (statusCode == 404)
{
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
else if (statusCode == 500)
{
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}
});
}
private String composeJSON() {
// TODO Auto-generated method stub
ArrayList<HashMap<String, String>> check_in_List;
check_in_List = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("subject_code",received_subject_code);
check_in_List.add(map);
Gson gson = new GsonBuilder().create();
return gson.toJson(check_in_List);
}
public void load_data()
{
ArrayAdapter<String> phy = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
l1.setAdapter(phy);
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
//String pos = (String) l1.getItemAtPosition(position);
int topic_index = position + 1;
String pos = String.valueOf(topic_index);
});
}
Upvotes: 0
Views: 816
Reputation: 8388
The error is due to these lines :
JSONObject jsonObject = new JSONObject();
list.add(jsonObject.toString());
Here you are not initializing the JSONObject from JSONArray , but you are creating a new one.
The solution could be to initialize it from JSONArray :
JSONObject jsonObject = arr.getJSONObject(i)
list.add(jsonObject.toString());
For parsing thing In your JSON nested JSONArrays are given.
you can try following code to parse it:
JSONArray arr = new JSONArray(response);
for (int i = 0; i < arr.length(); i++) {
JSONArray internalJSONArray=arr.getJSONArray(i);
for (int j=0;j<internalJSONArray.length();j++){
String data=internalJSONArray.getString(j);
System.out.println(data);
}
}
Upvotes: 1