Tải bản đầy đủ - 0 (trang)
Hack 80. Create a Yahoo! Local MIDlet

Hack 80. Create a Yahoo! Local MIDlet

Tải bản đầy đủ - 0trang

performlocalsearch,implementedoverYahoo!LocalSearch

API,fromamobiledevice.



5.5.1.WhatYouNeed

Tousethishack,you'llneedaHandspringTreo600runningthe

Web-SphereEveryplaceMicroEnvironment(WEME),which

you'llfindathttp://www.palmone.com/us/support/jvm.

You'llalsoneedtheSunJavaWirelessToolkitVersion2.2,which

youcanfindathttp://java.sun.com/products/j2mewtoolkit,and

theEclipsedevelopmentenvironmentwiththeEclipseMEpluginforbuildingMIDlets,availableathttp://eclipseme.org.



5.5.2.TheCode

Thecodeforthishackisorganizedintothreefiles:



LocalSearch.java

MIDletdescribedinmoredetailbelow



LocalSearchResponseHandler.java

SAXeventhandlerclassusedtoparsetheresponsefrom

Yahoo!LocalSearch



URLUTF8Encoder.java

UtilityclassusedtoencodespecialcharactersintheURL



Youcandownloadthecompletecodeforthishackathttp://mobilej2me.blogspot.com.



ThecodeinLocalSearch.javahandlesmostofthework.Thisfile

implementstheMIDletinterfacerequirements,describesthe

applicationinterface,andhandlesanyuserinteraction.Here'sa

lookatthekeymethodsinLocalSearch.java:

























































//package/importstatements

publicclassLocalSearchextendsMIDletimplementsComm

{



//Datamembers



publicLocalSearch()



{





//CreatetheUIformsandthebuttons/



}



protectedvoidstartApp()throwsMIDletStateCha



{





//Setthedisplaytothemainform



}



protectedvoidpauseApp()



{





//Notused



}



protectedvoiddestroyApp(booleanunconditional



{





//Notused



}



publicvoidcommandAction(Commandc,Displayabl



{





//HandlecommandsfromtheUI





//Retrievedataenteredbytheuser





//InvokeYahoo!LocalSearch(seedoYa





//Parsetheresponse













































































//Displaytheresultsorerrortothe

}

//InvokeYahooLocalSearchandgetresponse

privateStringdoYahooLocalSearch()throwsIOEx

{



//InvokeYahooLocalSearch



Stringurl=



"http://api.local.yahoo.com/LocalSearch



+"appid="



+appID



+"&query="



+URLUTF8Encoder.encode(query)



+"&results="



+results



+"&start="



+start





+"&zip="+zip;





















return(postViaHttpConnection(url).toFor

}

privateLocalSearchResponseHandlerpostViaHttpC























throwsIOException



{





//POSTaHTTPrequestwiththeURL





//Retrievetheresponse(XMLdocument)





//InvokeparseXML()



}



//ParsetheresponseXMLdocumentusingSAXan



//theneededinformation



privateLocalSearchResponseHandlerparseXML(Inp













SAXException,IOException



{



}

}



ThefollowingcodeisaSAXhandlerthatretrievesthetitle,

phonenumber,andaddressfromeachofthebusinessesthat

Yahoo!returns.Herearethekeymethodsin

LocalSearchResponseHandler.java:











//package/importstatements

classLocalSearchResponseHandlerextendsDefaultHandler

{



//Datamembers























Attributesattributes)throwsSAXException



{





//KeeptrackofQNames





qNameStack.push(qName);



















































//Getthe#ofresultsavailableandt

//returnedintheresponse

if(qName.equals(ELEM_RS))

{



totalResultsAvailable=attribute



totalResultsReturned=attribut

}



























}



currentElementContent="";













publicvoidcharacters(char[]ch,intstart,in





throwsSAXException

{

currentElementContent=currentElementContent+









publicvoidstartElement(Stringuri,Stringloc



length);











}















publicvoidendElement(Stringuri,StringlocalName,St





throwsSAXException

{



//GetcurrentQName



qName=(String)qNameStack.peek();



























































//Getthetitle,phone,addressofthelisting

if(ELEM_TITLE.equals(qName))

{



titles.addElement(currentElementContent

}

elseif(ELEM_PHONE.equals(qName))

{



phones.addElement(currentElementContent

}

elseif(ELEM_ADDRESS.equals(qName))

{



addresses.addElement(currentElementCont

}















}



//PopQName,sincewearedonewithit

qNameStack.pop();



}



5.5.3.RunningtheHack

Onceyouhavethesourcefilesinplace,youcanbuildthe

applicationfromwithinEclipsebyright-clickingtheprojectand

selectingBuildProject.Ortopackagetheapplication,right-click



theprojectandselectJ2ME



CreatePackage.



UploadtheresultingYahooWebServices.jarfiletoapublicly

accessiblewebserver.ThenusetheIBMMIDletmanageronthe

TreotodownloadtheMIDletattheappropriateURL.

Oncethecodeisinstalled,launchtheIBMMIDletmanagerfrom

thehandheld.Youshouldseeascreensimilartothatshownin

Figure5-6.



Figure5-6.MIDletmanager



YoucanalsousetheMIDletmanagertoinstalladifferentMIDletorget

anewversionoftheYahoo!WebServicesMIDlet.



Fromthemanager,selecttheYahooWebServicesMIDletand

clickLaunch.ThemainscreenoftheMIDletwillappear,as

showninFigure5-7.



Figure5-7.MIDletentryform



Therearefivefieldsyoucanfillinontheform.AppIDidentifies

theuserofYahoo!LocalWebServices.Bydefault,itsvalueis

settoYahooDemo,butyoucangetyourownappIDat

http://api.search.yahoo.com/webservices/register_application.

TheQueryfieldisforthebusinessyou'relookingfor;Resultsis

forthenumberofresultsthatyouwanttoseeontheresults

screen;andStartidentifiesthepointfromwhereyouwouldlike

toseetheresultsfromthecompletelist.Finally,ZIPisforthe

ZipCodethatidentifiesthelocationwhereyouwanttodothe

search.

Enterthenameofalocalbusinessortypeofbusinessalong

withyourZipCodeandclickSearch.Youshouldseeresultslike

thoseshowninFigure5-8.



Figure5-8.MIDletresults



Ifthetopresultsaren'tquitewhatyou'relookingfor,youcan

clickNexttoseemore.

Assemblingthishacktakesabitofwork,butit'sanexcellent

exampleofseamlesslyintegratingYahoo!dataintounusual

places,suchasapplicationsformobiledevices.

DeepakNadig



Hack81.ImportYahoo!LocalListingsintoExcel



BringingYahoo!LocalSearchresultsintoaspreadsheet

canhelpyousort,view,andvisualizelocalbusinesses.

HavingaccesstotheYahoo!LocalAPIislikebeingableto

effortlesslymoveentriesfromthephysicalYellowPagestoyour

computer.Anddumpingtheentriesintoastructured

environmentlikeExcelcangiveyouanewwaytolookat

Yahoo!LocalSearchresults.

ImaginethatyoufindyourselfinCorvallis,Oregon,andyou'd

liketofindaspottositdown,grababitetoeat,andsurfthe

Internetwirelessly.AYahoo!LocalSearchforwifinearthe

CorvallisZipCode(97333)willturnupseveralresultsfor

businessesthathavewirelessInternetaccess,asshownin

Figure5-9.



Figure5-9.Yahoo!LocalSearchfor"wifi"near

97333



Unfortunately,Yahoo!Localwon'ttellyouwhichplaceshave

freewirelessaccess,soyou'dhavetocalleachofthe

businessestoaskthem.Youcouldscrollandclickthroughthe

webpageandjotdowneachphonenumber,oryoucouldtake

advantageoftheYahoo!LocalAPItoimportthelistintoa

spreadsheetforquickreferencetothephonenumbers.



5.6.1.ExportingYahoo!LocalSearchtoa

Spreadsheet

ProgrammerRikulPatelhasalreadydonemuchofthework

involvedwithimportingresultsintoaspreadsheet.Hisprogram,

calledYahoo!LocalSearchToExcel,isavailableforfreeat

http://rikulpatel.com/ylocalsearch.php.It'savailableon

Windowsonly,andyou'llneedaspreadsheetprogramsuchas



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 80. Create a Yahoo! Local MIDlet

Tải bản đầy đủ ngay(0 tr)

×