I have been trying to parse Json into A ListView but when i do so I get;
03-04 14:43:45.345: E/log_tag(16519): Error parsing data org.json.JSONException: No value for items
It somehow can't get the items out of the Json Object.
Here's my code i used for it;
getJSON.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class getJSON {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("NO CONNECTION", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("CANT CONVERT DATA", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("CANT READ DATA", "Error parsing data "+e.toString());
}
return jArray;
}
}
and
the listview.java
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList> mylist = new ArrayList>();
//Get the data (see above)
JSONObject json = getJSON.getJSONfromURL("http://gdata.youtube.com/feeds/api/users/UKFDubstep/uploads?v=2&alt=jsonc");
try{
final JSONArray array = json.getJSONArray("items");
//Loop the Array
for(int i=0;i < array.length();i++){
HashMap map = new HashMap();
JSONObject e = array.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("title", "" + e.getString("title"));
map.put("viewCount", "Views: " + e.getString("viewCount"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.dubstep,
new String[] { "title", "viewCount" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap o = (HashMap) lv.getItemAtPosition(position);
Toast.makeText(dubstep.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
I have no idea where it went wrong.
Any help?
Thanks.
EDIT: json output
03-04 15:30:29.955: I/json(19109): {"error":{"message":"Response contains no content type","errors":[{"internalReason":"Response contains no content type","domain":"GData","code":"missingContentType"}],"code":400},"apiVersion":"2.1"}
Edit json link ;http://gdata.youtube.com/feeds/api/users/UKFDubstep/uploads?v=2&alt=jsonc