FilmInfoXmlPullParser类(HttpURLConnection超时间链接)

FilmInfoXmlPullParser类:

package hdp.li.fans.parser;

import hdp.li.fans.bean.MediaInfo_film;
import hdp.li.fans.bean.Media_addr_interface;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
//import android.annotation.SuppressLint;
import android.util.Log;

public class FilmInfoXmlPullParser extends XmlPullParserBase2 {

   private static final String TAG="FilmInfoXmlPullParser";
   
//    @SuppressLint("UseSparseArrays")
	public static MediaInfo_film getResourceList(String Url){
    	MediaInfo_film filmInfo= null;
    	ArrayList<Media_addr_interface> list=null;
    	XmlPullParser parser=null;
    	Media_addr_interface mi=null;
    	parser=getUriXmlPullParser(Url);
    	if(parser!=null){
    	try {
	    	int eventType=parser.getEventType();
	    	while(eventType!=XmlPullParser.END_DOCUMENT){
	    		switch(eventType){
	    		case XmlPullParser.START_DOCUMENT:
	    			Log.i(TAG, "start docment");
	    			filmInfo= new MediaInfo_film();
	    			list= new ArrayList<Media_addr_interface>();
	    			break;
	    		case XmlPullParser.START_TAG:
	    			String name= parser.getName();
	    			if(name.equalsIgnoreCase("did")){
	    				filmInfo.setTid(parser.nextText());
	    			}else if((name.equalsIgnoreCase("name"))&&(mi==null)){
	    				filmInfo.setName(parser.nextText());
	    			}else if(name.equalsIgnoreCase("actor")){
	    				filmInfo.setActor(parser.nextText());
	    			}else if(name.equalsIgnoreCase("director")){
	    				filmInfo.setDirector(parser.nextText());
	    			}else if(name.equalsIgnoreCase("myear")){
	    				filmInfo.setYear(parser.nextText());
	    			}else if(name.equalsIgnoreCase("mcomm")){
	    				filmInfo.setComm(parser.nextText());
	    			}else if(name.equalsIgnoreCase("score")){
	    				filmInfo.setScore(parser.nextText());
	    			}else if(name.equalsIgnoreCase("anothername")){
	    				filmInfo.setAnothername(parser.nextText());
	    			}else if(name.equalsIgnoreCase("lanage")){
	    				filmInfo.setLanauage(parser.nextText());
	    			}else if(name.equalsIgnoreCase("mlong")){
	    				filmInfo.setMlong(parser.nextText());
	    			}else if(name.equalsIgnoreCase("type")){
	    				filmInfo.setType(parser.nextText());
	    			}else if(name.equalsIgnoreCase("region")){
	    				filmInfo.setRegion(parser.nextText());
	    			}else if(name.equalsIgnoreCase("desc")){
	    				filmInfo.setDesc(parser.nextText());
	    			}else if(name.equalsIgnoreCase("mimg")){
	    				filmInfo.setImg(parser.nextText());
	    			}else if(name.equalsIgnoreCase("foobar")){
	    				mi= new Media_addr_interface();
	    			}else if((name.equalsIgnoreCase("name"))&&(mi!=null)){
	    				mi.setName(parser.nextText());
	    			}else if((name.equalsIgnoreCase("url"))&&(mi!=null)){
	    				mi.setUrl(parser.nextText());
	    			}
	    			break;
	    		case XmlPullParser.END_TAG:
	    			if(parser.getName().equalsIgnoreCase("foobar")){
	    				list.add(mi);
	    			}
	    			break;
	    		}
	    		eventType = parser.next();
	    	}
		} catch (MalformedURLException e) {
			Log.i(TAG, e.toString());
			return null;
		} catch (IOException e) {
			Log.i(TAG, e.toString());
			return null;
		} catch (XmlPullParserException e) {
			Log.i(TAG, e.toString());
			return null;
		}
    	filmInfo.setPlay_urls(list);
        return filmInfo;
      }
    	return null;
  }
    
}

XmlPullParser2类:返回一个XmlPullParser 对象给FilmInfoXmlPullParser类

package hdp.li.fans.parser;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;

public class XmlPullParserBase2 {
	private static final String TAG="XmlPullParserBase2";
	
	 public static XmlPullParser getUriXmlPullParser(String url) {
	        XmlPullParser parser = null;
	        try {
	            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	            factory.setNamespaceAware(true);
	            parser = factory.newPullParser();
	        } catch (XmlPullParserException e) {
	            Log.e(TAG, "Unable to create XmlPullParser", e);
	            return null;
	        }

	        InputStream inputStream = null;
	        HttpURLConnection conn=null;
	        try{
	          URL Url = new URL(url.toString());
              conn = (HttpURLConnection) Url.openConnection();
              conn.setDoInput(true);
              conn.setConnectTimeout(4000);                                    //四秒的超时间链接
              conn.connect();
              inputStream = conn.getInputStream();
//              String str=InputStreamTOString(inputStream,"utf-8");
//              Log.i(TAG, str);
              try {
            	  if(inputStream!=null)		
  	              parser.setInput(inputStream, "UTF-8");
  	          } catch (XmlPullParserException e) {
  	              Log.w(TAG, "Error while reading XML file from " + url, e);
  	              return null;
  	          }
	        }catch(Exception e){
	        	Log.i(TAG, e.toString()+"@@@@@@@@@@@@@@@@@@@");
	        }finally{
//	           conn.disconnect();
	        }
	        return parser;
	    }
}

FilmInfoXmlPullParser类(HttpURLConnection超时间链接)_第1张图片



你可能感兴趣的:(FilmInfoXmlPullParser类(HttpURLConnection超时间链接))