2012-05-10 174 views
1

我想構建一個基於android的twitter提要閱讀器應用程序。我在解析json響應時遇到了問題。這裏是我的代碼:解析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); 

} 

}

現在,我得到的消息:

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); 
      } 
     } 
+0

請告訴我們什麼'結果如下。 –

+0

@AmokraneChentir:我相信網址是公開的:http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5 – Amadan

回答

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更容易閱讀

+0

非常感謝,讓我更好地理解JSONObject&JSONArray!現在它在你的幫助下運作良好。 –