2012-08-04 57 views
2

我已經接到了一個教程如何JSON數據解析爲一個自定義列表視圖如何解析TwitterFeed(JSON)到自定義列表視圖?

下面代碼是從教程中的代碼與一對夫婦的修改:

public class TwitterParseHandler extends Activity { 
ArrayList<TwitterFeed> arrayOfWebData = new ArrayList<TwitterFeed>(); 

class TwitterFeed { 
    public String text; 
    public String created_at; 
} 

FancyAdapter aa=null; 

static ArrayList<String> resultRow; 

public void onCreate(Bundle savedInstanceState) { 
    try 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_twitter_parse_handler); 
     String result = ""; 

     try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True"); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      InputStream webs = entity.getContent(); 

      try 
      { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "/n"); 
       } 

       webs.close(); 
       result=sb.toString(); 
      }catch(Exception e){ 
       Log.e("log_tag", "Error in converting result "+e.toString()); 
      } 
     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     try 
     { 
      JSONArray jArray = new JSONArray(result); 
      for (int i=0;i<jArray.length();i++) 
      { 
       JSONObject json_data = jArray.getJSONObject(i); 
       TwitterFeed resultRow = new TwitterFeed(); 
       resultRow.text = json_data.getString("text"); 
       resultRow.created_at = json_data.getString("created_at"); 

       arrayOfWebData.add(resultRow); 
      } 
     }catch(JSONException e){ 
      Log.e("log_tag", "Error in parsing data "+e.toString()); 
     } 

     ListView myListView = (ListView)findViewById(R.id.list); 

     aa=new FancyAdapter(); 

     myListView.setAdapter(aa); 
    } 
    catch(Exception e){ 
     Log.e("ERROR", "ERROR IN CODE: "+e.toString()); 
     e.printStackTrace(); 
    } 
} 

class FancyAdapter extends ArrayAdapter<TwitterFeed> { 
    FancyAdapter() { 
     super(TwitterParseHandler.this, android.R.layout.simple_list_item_1, arrayOfWebData); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 

     if (convertView==null) { 
      LayoutInflater inflater=getLayoutInflater(); 
      convertView=inflater.inflate(R.layout.twitterlist, null); 

      holder=new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } 
     else 
     { 
      holder=(ViewHolder)convertView.getTag(); 
     } 
     holder.populateFrom(arrayOfWebData.get(position)); 

     return(convertView); 
    } 
} 

class ViewHolder { 
    public TextView name=null; 
    public TextView birthday=null; 

    ViewHolder(View row) { 
     name=(TextView)row.findViewById(R.id.TwitterText); 
     birthday=(TextView)row.findViewById(R.id.TwitterDatum); 
    } 

    void populateFrom(TwitterFeed r) { 
     name.setText(r.text); 
     birthday.setText(r.created_at); 
    } 
} 
} 

我從取代了JSON教程,我通過twitterfeed(這是做工精細):https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True

和更改的屬性從namebirthdaytextcreated_at,現在當我調試它,我得到以下錯誤:

08-04 12:36:40.539: E/log_tag(4599): Error in parsing data org.json.JSON 
Exception: Value 
{ "error": "This method requires a GET.", 
    "request":"\/1\/statuses\/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True" 
} of type org.json.JSONObject cannot be converted to JSONArray 

爲什麼會發生這種情況,我該如何解決?

+1

你試過'httpget'? 'HttpGet request = new HttpGet(「twitter url」);' – MrYanDao 2012-08-04 13:46:21

回答

1

嘗試:

HttpGet httpGet = new HttpGet(https://api.twitter.com/1/statuses/user_timeline.json); 

與參數:

List<NameValuePair> params = new LinkedList<NameValuePair>(); 
params.add(new BasicNameValuePair("include_entities", true)); 
... 

嘗試也How to add parameters to a HTTP GET request in Android?

+0

嘿,謝謝!,對於遲到的回覆是在度假,抱歉它工作HttpGet是問題,參數是不需要的。 – user1534326 2012-08-12 13:10:04

+0

沒問題,我很高興這對你有幫助;) – rule 2012-08-13 06:52:37