android parse json,Android: Parse Json in Listview

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值