论坛首页 Java企业应用论坛

java 获取 itunes AppStore Top100

浏览 2459 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-11-07   最后修改:2011-11-09
主要是想写读取图片自定义标签的。
写完以后 突然想做件奇怪事 通过自定义标签读取  AppStore Top 100.


package tag;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class AppTopTag extends TagSupport {

	private String url ;
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/** 
	 * httpjson   
	 * @Title: AppTopTag.java
	 * @Package tag
	 * @Description: TODO
	 * @author liazhou 
	 * @date 2011-11-7  23:11:11
	 * @version V1.0   
	 */
	private static final long serialVersionUID = 8253601591021627633L;

	@SuppressWarnings("unchecked")
	public int doEndTag() throws JspException {
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
		try {
			this.pageContext.getOut().print("<title>ax.itunes.apple.com  >AppTop 100</title>");
			httpClient.executeMethod(postMethod);
			//获取二进制的byte流
			//byte[] b = postMethod.getResponseBody();
			InputStream xmlStream = postMethod.getResponseBodyAsStream();
			InputStreamReader xmlStreamReader = new InputStreamReader(xmlStream, "UTF-8");
			if (xmlStreamReader != null) {
				SAXReader saxReader = new SAXReader();
				Document document = saxReader.read(xmlStream);
				document.setXMLEncoding("UTF-8"); 
				Element root  = document.getRootElement();
				StringBuffer sbhtml = new StringBuffer();
				//找出所有的entry
				for(Iterator<Element> i = root.elementIterator("entry");i.hasNext();){
					Element element =  i.next(); 
					Element elementc = element.element("content");
					String en = elementc.getText();
					if(null!=en && !"".equals(en))
						sbhtml.append(en);
				}
				this.pageContext.getOut().print(sbhtml);
				// 遍历子节点元素
		    }
		}catch (Exception e) {
			e.getMessage();
		}
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return Tag.SKIP_BODY; //不处理正文内容  
	}
}





apptop.jsp
<%@taglib uri="/union-ui-tag" prefix="union-ui"%>
<union-ui:appTop url="http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/sf=143441/limit=100/xml"></union-ui:appTop>



union-tags.tld

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

<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
	version="2.1">
	
	
  <display-name>"Union Tags"</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>union-ui</short-name>
  <uri>/union-ui-tag</uri>
  <tag>
    <description><![CDATA[Execute an action from within a view]]></description>
    <name>img</name>
    <tag-class>tag.ImageTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <description></description>
      <name>id</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  <tag>
  	<name>appTop</name>
 	<tag-class>tag.AppTopTag</tag-class> 
 	<body-content>JSP</body-content>
 	<attribute>
 		<name>url</name>
 		<required>true</required>
 		<rtexprvalue>true</rtexprvalue>
 	</attribute>
  </tag>
</taglib>



页面截图


  • 大小: 88.2 KB
   发表时间:2011-11-09  
提供下 url 地址
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS

http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/sf=143441/limit=100/xml
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics