Pages

Sunday, March 17, 2013

Retrieve tweets from twitter using button and JSON


XML
Don't forget to add Internet Permission. 





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Username of Twitter:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnShowTweets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showTweets"
        android:text="Show Tweets!" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/tvJson"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tweets:" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>




JAVA (MainActivity.class)



import java.io.IOException;
import java.util.ArrayList;

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.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
String completeData = "";
TextView tv;
EditText et;
Button btn;

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

//used to get username from edit text
String user_namee =null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tvJson);
et = (EditText)findViewById(R.id.etUsername);
btn = (Button)findViewById(R.id.btnShowTweets);

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
user_namee = et.getText().toString();

if (user_namee.equals("")) {
Toast.makeText(getApplicationContext(), "Enter a valid twitter id",
Toast.LENGTH_LONG).show();
return;
}

Read rd = new Read();
rd.execute("text");

}
});

}

public JSONArray lastTweet(String username) throws ClientProtocolException , IOException , JSONException{
client = new DefaultHttpClient();
StringBuilder urll = new StringBuilder(URL);

urll.append(username);


HttpGet get = new HttpGet(urll.toString());
HttpResponse r = client.execute(get);

int status = r.getStatusLine().getStatusCode();
// 200 is sucesful
if (status == 200) {
HttpEntity entity = r.getEntity();
String data = EntityUtils.toString(entity);
JSONArray timeline = new JSONArray(data);

return timeline;

}
else


{

Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_SHORT).show();
return null;

}


}

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


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

ArrayList<String> al = new ArrayList<String>();
try {
// string username 
jsoon = lastTweet(user_namee);

int count = jsoon.length();
JSONObject jobj = new JSONObject();

for (int i = 0; i < count; i++) {

jobj = jsoon.getJSONObject(i);

al.add(jobj.getString("text").toString());
}
return al ;

} 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(ArrayList<String> al) {
// TODO Auto-generated method stub


for (String string : al) {

completeData += string + System.getProperty("line.separator")
+ System.getProperty("line.separator");
}
tv.setText("Tweets : " + System.getProperty("line.separator")
+ completeData);

}

}
}





No comments:

Post a Comment