Code/Android

안드로이드 코딩 : 아이디 검색후 트위터 팔로어 출력(TwitterSCKR ver 0.2)(Twitter, My followers List on Android)

sidcode 2010. 8. 14. 17:31



1. mini SDK-VERSION
android 2.1-update1


2. Main UI ( main.xml )

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:id="@+id/mainLayout"

android:layout_width="fill_parent" android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:id="@+id/searchT" android:layout_width="210px"

android:layout_height="wrap_content" android:text="" android:textSize="18sp"

android:layout_x="14px" android:layout_y="14px">

</EditText>

<Button android:id="@+id/searchB" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Search"

android:layout_x="249px" android:layout_y="13px">

</Button>

<ScrollView android:id="@+id/followersView"

android:layout_width="308px" android:layout_height="300px"

android:layout_x="7px" android:layout_y="83px">

<TextView android:id="@+id/showText"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    />

</ScrollView>

</AbsoluteLayout>




3. Main Code ( ShowUser.java )

package com.TwitterSCKR;


import java.net.URL;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


public class ShowUser extends Activity {

XMLTagReturn xmltr = null;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button btn = (Button)findViewById(R.id.searchB);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

EditText eText = (EditText)findViewById(R.id.searchT);

TextView name[];

TextView location[];

TextView description[];

try {

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

URL xmlUrl = new URL("http://api.twitter.com/1/statuses/followers.xml?screen_name=" + eText.getText().toString()); // 팔러워 모두 

//URL xmlUrl = new URL("http://api.twitter.com/1/statuses/show.xml?screen_name=sidcode"); // 개인 프로필

//URL xmlUrl = new URL("http://api.twitter.com/1/users/lookup.xml?screen_name=sidcode,brantdaugherty"); // 동시에 여러 유저


MyXMLHandler xmlHd = new MyXMLHandler();

xr.setContentHandler(xmlHd);

xr.parse(new InputSource(xmlUrl.openStream()));

} catch (Exception e) {

System.out.println(e);

}

xmltr = MyXMLHandler.xmltr;


name = new TextView[xmltr.getName().size()];

//location = new TextView[xmltr.getLocation().size()];

//description = new TextView[xmltr.getDescription().size()];

TextView allFollowers = (TextView)findViewById(R.id.showText);

String a = "";

for(int i = 0; i < xmltr.getName().size(); i++){

a += "이름 : " + xmltr.getName().get(i) + "\n " +

"지역 : " + xmltr.getLocation().get(i) + "\n " +

"소개글 : " + xmltr.getDescription().get(i) + "\n\n";

}

allFollowers.setText(a);

}

});


}

}



4. XML Handler ( MyXMLHandler.java )

package com.TwitterSCKR;


//import org.xml.sax.AttributeList;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;


public class MyXMLHandler extends DefaultHandler {

boolean cElement = false;

String cValue = null;

public static XMLTagReturn xmltr = null;


public static XMLTagReturn getXMLTagReturn(){

return xmltr;

}


public static void setXMLTagReturn(XMLTagReturn xmltr){

MyXMLHandler.xmltr = xmltr;

}


public void startElement(String uri, String localName, String tagName, Attributes attribute) throws SAXException{

cElement = true;


//if(localName.equals("user")){ // 개인 유저 프로필 볼때 

if(localName.equals("users")){ // 해당 유저의 팔로어 전체 !! 

xmltr = new XMLTagReturn();

}

}


public void endElement(String uri, String localName, String tagName) throws SAXException {

cElement = false;

if(localName.equalsIgnoreCase("screen_name")){

xmltr.setName(cValue);

} else if(localName.equalsIgnoreCase("location")) {

xmltr.setLocation(cValue);

} else if(localName.equalsIgnoreCase("description")){

xmltr.setDescription(cValue);

}

}


public void characters(char[] ch, int start, int length) throws SAXException {

if (cElement) {

cValue = new String(ch, start, length);

cElement = false;

}

}

}




5. ArrayList & Get, set ( XMLTagReturn.java )

package com.TwitterSCKR;


import java.util.ArrayList;


public class XMLTagReturn {

private ArrayList<String> name = new ArrayList<String>();

private ArrayList<String> location = new ArrayList<String>();

private ArrayList<String> description = new ArrayList<String>();

public ArrayList<String> getName() {

return name;

}

public void setName(String name) {

this.name.add(name);

}

public ArrayList<String> getLocation() {

return location;

}

public void setLocation(String location) {

this.location.add(location);

}


public ArrayList<String> getDescription() {

return description;

}


public void setDescription(String description) {

this.description.add(description);

}

}




6. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.TwitterSCKR"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".ShowUser"

                  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-permission android:name="android.permission.INTERNET"></uses-permission>


</manifest> 



드디어 Text 수준에서 어설프지만 UI 란걸 아주 극미세 하게 구현해보았다.

android:id="@+id/showText" 를... 
android:text="@+id/showText" 라고 적는..
바보는 없겠지.. 절대 난 안그랫음.. 그래서 한시간동안.. 화낸적없음!!!!!!!!

ps. 이제 안드로이드 코딩에 조금은 익숙해 져 진듯하다.... =)