ソース
Kitwi.java
これが画面表示を行うクラス
/*10/13 Kitwiに名前を変更。Tは発音しないでキウイ*/ package per.neet.kitwi; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.LinearLayout; import android.widget.TextView; import android.view.View; public class Kitwi extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); TextView message = new TextView(this); message.setText("パブリックタイムラインを取得します..."); layout.addView(message); setContentView(layout); TextView tv = new TextView(this); try { String value = new String(TwitterApi.getPublicTimeline()); Log.d("kitwi", "value of Kitwi.value is "+value); tv.setText(value); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } layout.addView(tv); setContentView(layout); // TextView[] tvArray = new TextView[5]; // // for (int i = 0; tvArray.length > i; i++){ // Log.d("kitwi", "loop no."+i); // tvArray[i] = new TextView(this); // tvArray[i].setText("こんにちは世界! no."+i); // layout.addView(tvArray[i]); // } // setContentView(layout); } }
XmlHandler.java
package per.neet.kitwi; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import android.util.Log; public class XmlHandler extends DefaultHandler{ //変数を用意する boolean isName, isText, isCreated; StringBuffer name = new StringBuffer(); StringBuffer text = new StringBuffer(); StringBuffer created = new StringBuffer(); // String[] strArray = new String[20]; //公開タイムラインの一度の取得数は20 int i = 0; StringBuffer strbuf = new StringBuffer(); final static String tag = "kitwi"; //LogCat向けのタグ //文書の開始。フラグを初期化 public void startDocument(){ // strbuf = new StringBuffer(); } //文書の終了 public void endDocument(){/*何もしない*/} //要素の開始 public void startElement( String uri, String localName, //ローカルネーム String qName, //要素名 Attributes attributes){ //属性 //3要素のフラグを立てる if (localName == "name"){ //ID isName = true; Log.d(tag, "name flag: true"); // name = new StringBuffer(); }else if (localName == "text"){ //POST内容 isText = true; Log.d(tag, "text flag: true"); // text = new StringBuffer(); }else if (localName == "created_at"){ //時間 isCreated = true; Log.d(tag, "created flag: true"); // created = new StringBuffer(); } } //要素の終了 public void endElement( String uri, String localName, String qName){ // Log.d(tag, "endElement: qName is "+localName); //3要素のフラグを倒す if (localName == "name"){ //ID isName = false; Log.d(tag, "endElement: localName is name"); }else if (localName == "text"){ //POST内容 isText = false; Log.d(tag, "endElement: localName is text"); }else if (localName == "created_at"){ //時間 isCreated = false; Log.d(tag, "endElement: localName is created"); }else if (localName == "status"){ Log.d(tag, "XmlHandler.strbuf.append("+name+" : "+text+" at "+created+")"); strbuf.append(name+" : "+text+" at "+created+"\n"); name.delete(0, name.length()); text.delete(0, text.length()); created.delete(0, created.length()); //strArray[0]-[19] // if (i < 20){ // strArray[i] = new String(name+" : "+text+" at "+created); // Log.d("kitwi", "strArray[i++] @XmlHandler:54"); // i++; // } } } //文字列を扱う public void characters(char[] ch, int start, int length){ if (length > 0){ if (isName){ Log.d(tag, "get chracters: "+new String(ch, start, length)); name.append(ch, start, length); Log.d(tag, "characters.isName: "+name); }else if (isText){ Log.d(tag, "get chracters: "+new String(ch, start, length)); text.append(ch, start, length); }else if (isCreated){ Log.d(tag, "get chracters: "+new String(ch, start, length)); created.append(ch, start, length); } } } /*上で溜め込んだstatusの配列を吐き出すメソッド * 自動的には実行されない*/ public String out(){ String string = new String(strbuf.toString()); Log.d("kitwi", "value of string is "+string); return string; } }
TwitterApi.java
package per.neet.kitwi; import java.io.BufferedInputStream; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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.xml.sax.SAXException; import android.util.Log; public class TwitterApi extends DefaultHttpClient{ final static String base_uri = "http://twitter.com/"; //Twitter API final static String debug_uri = "http://10.0.2.2/"; //Emulatorから見たlocalhost final static String status_uri = "statuses/"; //ステータス関連のAPIを示す final static String tag = "kitwi"; public static String getPublicTimeline() throws ParserConfigurationException, SAXException, IOException{ String uri = base_uri+status_uri+"public_timeline.xml"; String statuses = new String(Getter(uri)); return statuses; } //引数で受け取ったuriからGETしてレスポンスの本文部分のInputStreamをParseへ渡す。 //帰ってきたString[]を戻り値とする。例外はStringの中で返す private static String Getter(String uri) throws ParserConfigurationException, SAXException, ClientProtocolException, IOException{ HttpClient client = new DefaultHttpClient(); HttpGet method = new HttpGet(uri); //メソッド String statuses = null; BufferedInputStream contentStream = null; HttpResponse response = null; try { //clientでGetを実行。レスポンスからStatusLine->StatusCode(サーバーの応答の種類を示す)を取り出す //cf."6.1 Status-Line" http://www.ietf.org/rfc/rfc2616.txt response = client.execute(method); int statuscode = response.getStatusLine().getStatusCode(); //StatusCodeは数字なのでint //リクエストが成功 if (statuscode == HttpStatus.SC_OK){ //「200 OK」かどうか contentStream = new BufferedInputStream( response.getEntity().getContent()); statuses = new String(TwitterApi.Parse(contentStream)); } else{ // "サーバーエラー:"+statuscode; } }catch (RuntimeException e) { // RuntimeErrorの時は接続AdortしろとApache様が仰せです method.abort(); e.printStackTrace(); }finally{ try { //接続を解放する contentStream.close(); Log.d(tag, "contentStream.close(): success!!"); } catch (IOException e) { // 接続解放に伴うエラー e.printStackTrace(); } } return statuses; } //XMLパーサーを呼び出すクラス //closeはこの中ではやらない private static String Parse(BufferedInputStream in) throws ParserConfigurationException, SAXException, IOException { SAXParserFactory spf =SAXParserFactory.newInstance(); XmlHandler handl = new XmlHandler(); SAXParser saxparser; saxparser = spf.newSAXParser(); saxparser.parse(in, handl); return handl.out(); } }