Minggu, 15 Juli 2012

Menampilkan Twitter Timeline di Android Tanpa Oauth (Pemrograman Android)

Kali ini penulis ingin membagi sedikit ilmu untuk menampilkan Tweet langsung tanpa Oauth, tetapi menggunakan JSON. menggunakan fasilitas dari search.twitter.com yang bisa memberikan tweet atau timeline yang kita inginkan dengan keyword tertentu tanpa harus login terlebih dahulu.

Caranya buat project Android baru di Eclipse, kemudian ubah tampilan di main.xml menjadi seperti berikut ini :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" />
<ScrollView 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/result" />
</ScrollView>
</LinearLayout>



Kemudian pada MainActivity tambahkan kode program berikut :


package com.amijaya.readtweetjson;

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView result = (TextView)findViewById(R.id.result);

ArrayList<String> hsl = this.fetchTwitterTimeline("jogja");
String semua = "";
for (int z=0;z<hsl.size()-1;z++)
{
semua += hsl.get(z) + "\n";
}

result.setText(semua);
}

public ArrayList<String> fetchTwitterTimeline(String query) {
ArrayList<String> listItems = new ArrayList<String>();

String host = "search.twitter.com";
String twitterURL = "http://" + host + "/search.json?q=%23" + query
+ "&rpp=25&include_entities=true&result_type=recent";

try {

HttpClient client = new DefaultHttpClient();
BasicHttpContext localContext = new BasicHttpContext();
HttpHost targetHost = new HttpHost(host, 80, "http");
HttpGet httpget = new HttpGet(twitterURL);
httpget.setHeader("Content-Type", "application/json");

HttpResponse hresponse = client.execute(targetHost, httpget,
localContext);

HttpEntity entity = hresponse.getEntity();

Object content = EntityUtils.toString(entity);
Log.d("Test", "OK: " + content.toString());

JSONObject jo1 = new JSONObject(content.toString());
JSONArray ja = jo1.getJSONArray("results");

for (int i = 1; i <= ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
listItems.add("@" + jo.getString("from_user") + " : \n"
+ jo.getString("text"));
}
} catch (Exception e) {
e.printStackTrace();
}

return listItems;
}

}


Dan terakhir jangan lupa berikan permission INTERNET pada AndroidManifest.xml agar aplikasi yang dihasilkan mampu melakukan koneksi ke Internet :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amijaya.readtweetjson"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest> 


Hasilnya :


Untuk project selengkapnya dapat didownload disini. Setelah masuk Google Docs, silakan klik File - Download

Semoga berguna.