?? flickrps.java
字號:
import java.net.URL;
import org.w3c.dom.*;
//one sample application using flickr.photos.search method to perform a simple key word search
public class FlickrPS{
String api_key;
//1---REST
//2---SOAP
Integer requestformat=1;
String method="flickr.photos.search";
String tag;
String para="tags";
Integer no_display;
URL url;
Document doc;
public FlickrPS (String api_key, Integer requestformat,String tag,Integer no_display){
this.api_key = api_key;
this.requestformat=requestformat;
this.tag=tag;
this.no_display = no_display;
}
// simulated client
public static void main(String args[]){
// set the firewall proxy to pass the SIT firewall
// comment out the following three lines if you are using the program at home
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "www-cache.cs.usyd.edu.au");
System.setProperty("http.proxyPort","8000");
FlickrPS flickrps=new FlickrPS("8447cdc43d7b3e85c7d01f5d9875ecb9",Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]));
flickrps.searchByTag();
}
/**
* Assumming only one tag is used to search
*
*/
public void searchByTag(){
try {
//REST request
if(requestformat==1){
//using factory MessageFactory to create URL for REST request
this.url = new URL(MessageFactory.getRESTUrl(method,para,tag,api_key));
//using strategy to set communiction between client and server
//then get the response from server
CommFormat commformat = new CommFormat(new ConcreteStrategyREST(url));
this.doc=commformat.getResponse();
System.out.println("Getting Response from server successfully with REST!");
}
else if(requestformat==2){
this.url=new URL("http://api.flickr.com/services/soap/");
//using factory MessageFactory to create SOAPEnvelope for SOAP request
CommFormat commformat = new CommFormat(new ConcreteStrategySOAP(url,MessageFactory.getSOAP(method,para,tag,api_key)));
this.doc=commformat.getResponse();
System.out.println("Getting Response from server successfully with SOAP!");
}
else{
System.out.println("Invalid RequestFormat name!"+requestformat);
return;
}
//since when i use concretestrategy to get the response from server, for REST and SOAP,
//the response has been converted to Document formate,then i can using the same parsing method for both
//request format
//but because the two different method flickr.photos.search and flickr.tags.getrelated give the two different
//response in Document formate,so i am using the facade pattern for different method
//"PhotoPS"-----means using flickr.photos.search
//"PhotoTR"-----means using flickr.tags.getrelated
ParsingFacade facade = new ParsingFacade(doc,"PhotoPS");
//parsing response Document, then display it
facade.parsingPS(no_display);
} catch (Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -