Android 解析json时出错

Android 解析json时出错,android,json,parsing,twitter,arrays,Android,Json,Parsing,Twitter,Arrays,我想构建一个基于android的twitter提要阅读器应用程序。我在解析json响应时遇到问题。这是我的密码: `公共类HttpClientActivity扩展活动{ class Tweet{ public String username; public String message; } ArrayList<Tweet> tweets = new ArrayList<Tweet>(); static ArrayList<String>

我想构建一个基于android的twitter提要阅读器应用程序。我在解析json响应时遇到问题。这是我的密码:

`公共类HttpClientActivity扩展活动{

class Tweet{
    public String username;
    public String message;
}

ArrayList<Tweet> tweets = new ArrayList<Tweet>();

static ArrayList<String> resultRow;

@Override
public void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    public class Getdata {

public String getInternetData() throws Exception{

    String data = null;

    try {
        URI website = new URI("http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        data = EntityUtils.toString(entity);

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection: " +e.toString());
    }

    return data;
    }

    }

    Getdata getdata = new Getdata();
    String returned = null;
    try {
        returned = getdata.getInternetData();
    } catch (Exception e) {
        e.printStackTrace();
    }


    try{
        JSONArray jarray = new JSONArray(returned);
        for(int i=0; i<jarray.length(); i++){
            JSONObject jo = jarray.getJSONObject(i);
            Tweet tt = new Tweet();
            tt.username = jo.getString("from_user");
            tt.message = jo.getString("text");
            tweets.add(tt);
        }
    }
    catch(JSONException e){
        Log.e("log_tag", "Error parsing data: "+e.toString());
    }

    ListView lv = (ListView) findViewById(R.id.listView1);



    class FancyAdapter extends ArrayAdapter<Tweet> {

        public FancyAdapter() {
            super(HttpClientActivity.this, android.R.layout.simple_list_item_1, tweets);
        }

        public View getView(int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            if(convertView == null){
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.listitem, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.populatefrom(tweets.get(position));
            return(convertView);
        }


        class ViewHolder {
            public TextView username = null;
            public TextView message = null;

            ViewHolder(View listitem){
                username = (TextView)listitem.findViewById(R.id.username);
                message = (TextView)listitem.findViewById(R.id.message);
            }

            void populatefrom(Tweet t){
                username.setText(t.username);
                message.setText(t.message);
            }
        }
    }

    FancyAdapter ar = new FancyAdapter();
    lv.setAdapter(ar);

}
class Tweet{
公共字符串用户名;
公共字符串消息;
}
ArrayList tweets=新建ArrayList();
静态ArrayList resultRow;
@凌驾
创建时的公共void(Bundle savedInstanceState){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
公共类Getdata{
公共字符串getInternetData()引发异常{
字符串数据=null;
试一试{
URI网站=新的URI(“http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5");
HttpClient=new DefaultHttpClient();
HttpGet请求=新建HttpGet();
setURI(网站);
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
数据=EntityUtils.toString(实体);
}捕获(例外e){
e(“Log_标记”,“http连接错误:”+e.toString());
}
返回数据;
}
}
Getdata Getdata=新建Getdata();
返回的字符串=null;
试一试{
返回=getdata.getInternetData();
}捕获(例外e){
e、 printStackTrace();
}
试一试{
JSONArray jarray=新JSONArray(返回);

对于(int i=0;i要获得JSONArray,首先需要:

包含数组的JSON对象示例:

 String returned = "{ "myArray" : [item1, item2] }"

 JSONObject jsonObj = new JSONObject(returned);

 JSONArray jarray = jsonObj.getJSONArray("myArray");
使用以下示例:


如果你用这样的格式化程序:它的10万倍易于阅读

请告诉我们“结果”是什么样子。@AmokraneChentir:我相信URL是公开的:非常感谢你让我更好地理解JSONObject和JSONArray!现在在你的帮助下它工作得很好。
 String returned = "{ "myArray" : [item1, item2] }"

 JSONObject jsonObj = new JSONObject(returned);

 JSONArray jarray = jsonObj.getJSONArray("myArray");
 JSONObject jsonObj = new JSONObject(returned);
 JSONArray jarray = jsonObj.getJSONArray("results");
 JSONObject firstResult = jarray.getJSONObject(0); // loop if you want more
 String username = firstResult.getString("from_user");
 String message = firstResults.getString("text");