2012-03-31 105 views
0

我得到了這段代碼的工作原理,它返回了我兄弟的最後一條推文,我希望它能返回最後3條推文可以幫助我嗎?即時通訊不是很好的Java或XML我只是遵循mybringback幾個教程,請忽略我的壞英語。我認爲這個問題是在行:JSONObject last = timeline.getJSONObject(0);這使得它返回第一個推文,所以我如何改變這個,所以它返回第一個3? (0,1,2)不允許:(我如何解析最後3條推文而不是最後1條json

package net.thinkbin; 

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class menu extends Activity { 
TextView httpStuff; 
HttpClient client; 
JSONObject json; 


final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    httpStuff = (TextView) findViewById(R.id.tvHttp1); 
    client = new DefaultHttpClient(); 
    new Read().execute("Title"); 

    Button view = (Button) findViewById(R.id.button1); 
    view.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("net.thinkbin.TUTORIAL1")); 
      finish(); 
     } 
    }); 

    Button share = (Button) findViewById(R.id.button2); 
    share.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("net.thinkbin.SHARE")); 
      finish(); 
     } 
    }); 

} 

public JSONObject lastTweet(String username) 
     throws ClientProtocolException, IOException, JSONException{ 
    StringBuilder url = new StringBuilder(URL); 
    url.append(username); 

    System.out.println(url.toString()); 


    HttpGet get = new HttpGet(url.toString()); 
    HttpResponse r = client.execute(get); 
    int status = r.getStatusLine().getStatusCode(); 
    if (status == 200){ 
     HttpEntity e = r.getEntity(); 
     String data = EntityUtils.toString(e); 
     JSONArray timeline = new JSONArray(data); 

     JSONObject last = timeline.getJSONObject(0); 

     return last; 
    }else{ 
     Toast.makeText(menu.this, "error", Toast.LENGTH_SHORT); 
     return null; 
    } 
} 




public class Read extends AsyncTask<String, Integer, String>{ 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

       try { 
        json = lastTweet("koen*****"); 
        return json.getString("params[0]"); 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 



     return null;     
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     httpStuff.setText(result); 
    } 


} 
} 

回答

0

使用DilSe URL和嘗試這個代碼。

public JSONArray lastTweet(String username) 
     throws ClientProtocolException, IOException, JSONException{ 
    StringBuilder url = new StringBuilder(URL); 
    url.append(username); 

    System.out.println(url.toString()); 


    HttpGet get = new HttpGet(url.toString()); 
    HttpResponse r = client.execute(get); 
    int status = r.getStatusLine().getStatusCode(); 
    if (status == 200){ 
     HttpEntity e = r.getEntity(); 
     String data = EntityUtils.toString(e); 
     return new JSONArray(data); 
    }else{ 
     Toast.makeText(menu.this, "error", Toast.LENGTH_SHORT); 
     return null; 
    } 
} 




public class Read extends AsyncTask<String, Integer, String>{ 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

       try { 
        JSONArray timeline = lastTweet("koen*****"); 
        return timeline.getJSONObject(0).getString("params[0]")+" : " 
            +timeline.getJSONObject(1).getString("params[0]")+" : " 
            +timeline.getJSONObject(2).getString("params[0]"); 

       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 



     return null;     
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     httpStuff.setText(result); 
    } 
+0

它返回空白,但代碼看起來不錯! – 2012-03-31 16:37:02

+0

然後調整它並學習一些編程和Java。 – 2012-03-31 16:55:14

+0

我得到它的工作(我的一面愚蠢)謝謝!只有當我希望推特被置於彼此之下時,我應該改變什麼「:」呢? – 2012-03-31 17:19:33

相關問題