1

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

`公共类 HttpClientActivity 扩展 Activity {

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);

}

}

现在我得到了味精:

org.json.JSONException:java.lang.String 类型的值无法转换为 JSONArray

我不知道如何修复它:

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);
            }
      }
4

1 回答 1

2

要获得 JSONArray,您首先需要一个JSONObject

http://www.json.org/

包含数组的示例 JSON 对象:

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

 JSONObject jsonObj = new JSONObject(returned);

 JSONArray jarray = jsonObj.getJSONArray("myArray");

使用您的示例:http ://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5

 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");

如果你把它通过这样的格式化程序:http: //jsonformatter.curiousconcept.com/它的 100000x 更容易阅读

于 2012-05-10T21:52:04.697 回答