xml文件下载到本地—XmlPullParser解析下载到本地的xml(vlc)
2013-07-18 15:09
363 查看
使用下载类:
文件下载到本地的类:
解析本地的xml和接口的xml不同之处就是 XmlPullParser的实例parser设置的输出流是:parser.setInput(inputStream, null);的inputStream:
本地的流:
接口的流:
本地解析:
XmlFileDownloadThread p2pFileThread = new XmlFileDownloadThread( "http://my.7po.com/api/check/yuanli.php?macnum=" + mac, true, VideoPlayerActivity.this); XmlFileDownloadThread tvFileThread = new XmlFileDownloadThread( Constant.url_tv_new, false, VideoPlayerActivity.this); p2pFileThread.execute(); tvFileThread.execute();
文件下载到本地的类:
package com.qipo.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.content.Context; import android.os.AsyncTask; public class XmlFileDownloadThread extends AsyncTask<String, Integer, Void>{ private String mSavePath = null; private String urlString = null; private boolean tagP2p; private Context mContext; private File xmlFile = null; public XmlFileDownloadThread(String urlString, boolean tagP2p, Context mContext) { this.urlString = urlString; this.tagP2p = tagP2p; this.mContext = mContext; } protected Void doInBackground(String... params) { try { // if // (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) // { // String sdpath = Environment.getExternalStorageDirectory() + "/"; String sdpath = mContext.getFilesDir().toString() + "/"; mSavePath = sdpath + "qipotv"; URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); File file = new File(mSavePath); if (!file.exists()) { file.mkdir(); chmodPath(file.getAbsolutePath()); } if (tagP2p) { xmlFile = new File(mSavePath, "7pop2p.xml"); } else { xmlFile = new File(mSavePath, "7potv.xml"); } if (xmlFile.exists()) { xmlFile.delete(); } xmlFile.createNewFile(); FileOutputStream fos = new FileOutputStream(xmlFile); int numread = 0; byte buf[] = new byte[1024]; while ((numread = is.read(buf)) > 0) { fos.write(buf, 0, numread); } fos.close(); is.close(); // } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { if (xmlFile.exists()) { xmlFile.delete(); } e.printStackTrace(); } return null; } // public void run() { // try { // String sdpath = mContext.getFilesDir().toString() + "/"; // mSavePath = sdpath + "qipotv"; // // URL url = new URL(urlString); // HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // conn.connect(); // InputStream is = conn.getInputStream(); // // File file = new File(mSavePath); // if (!file.exists()) { // file.mkdir(); // chmodPath(file.getAbsolutePath()); // } // // if (tagP2p) { // xmlFile = new File(mSavePath, "7pop2p.xml"); // } else { // xmlFile = new File(mSavePath, "7potv.xml"); // } // if (xmlFile.exists()) { // xmlFile.delete(); // } // xmlFile.createNewFile(); // FileOutputStream fos = new FileOutputStream(xmlFile); // int numread = 0; // byte buf[] = new byte[1024]; // // while ((numread = is.read(buf)) > 0) { // fos.write(buf, 0, numread); // } // fos.close(); // is.close(); // } catch (MalformedURLException e) { // e.printStackTrace(); // } catch (IOException e) { // if (xmlFile.exists()) { // xmlFile.delete(); // } // e.printStackTrace(); // } // } public static boolean chmodPath(String path) { boolean issuccess = false; Process process = null; try { process = Runtime.getRuntime().exec("chmod 777 " + path); issuccess = process.waitFor() == 0; } catch (Exception e) { e.printStackTrace(); issuccess = false; } return issuccess; } }
解析本地的xml和接口的xml不同之处就是 XmlPullParser的实例parser设置的输出流是:parser.setInput(inputStream, null);的inputStream:
本地的流:
String sdpath =mContext.getFilesDir().toString()+"/"; String mSavePath = sdpath + "qipotv"; File xmlFile = new File(mSavePath, "7potv.xml"); try{ if(xmlFile.exists()){ inputStream = new FileInputStream(xmlFile); }else{ return null; }
接口的流:
InputStream inputStream = null; try { final HttpPost post=new HttpPost(url); // final HttpGet get = new HttpGet(url); HttpResponse response = mHttpClient.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); if (entity != null) { inputStream = entity.getContent(); } } } catch (IOException e) { Log.w(TAG, "Error while retrieving XML file " + url, e); return null; }
本地解析:
package com.qipo.api;解析的xml格式:
import com.qipo.bean.Channel;
import com.qipo.bean.TvList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.net.http.AndroidHttpClient;
import android.os.Environment;
import android.util.Log;
public class TvListDownloadPullParser extends XmlPullParserBase {
private static final String TAG = "TvListXmlPullParser";
public static TvList getTvList(Context mContext) {
TvList tvlist = new TvList();
ArrayList<Channel> channelList = new ArrayList<Channel>();
AndroidHttpClient mHttpClient = AndroidHttpClient
.newInstance("Android");
Channel element=null;
ArrayList<String> urlList=null;
XmlPullParser parser = null;
FileInputStream inputStream=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;
}
// if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
// {
// String sdpath = Environment.getExternalStorageDirectory() + "/";
String sdpath =mContext.getFilesDir().toString()+"/"; String mSavePath = sdpath + "qipotv"; File xmlFile = new File(mSavePath, "7potv.xml"); try{ if(xmlFile.exists()){ inputStream = new FileInputStream(xmlFile); }else{ return null; }
}catch(FileNotFoundException e){
Log.w(TAG, "Error while new XML file from ", e);
return null;
}
try {
if(inputStream!=null){ //xlh_add
parser.setInput(inputStream, null);
}
} catch (XmlPullParserException e) {
Log.w(TAG, "Error while reading XML file from ", e);
return null;
}
// }
if (parser != null) {
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = parser.getName();
if (name.equals("header")) {
tvlist.setServer(parser.getAttributeValue(null,
"server"));
String s = parser
.getAttributeValue(null, "classes");
if (s != null) {
HashMap<Integer, String> menu = new HashMap<Integer, String>();
ArrayList<Integer> ids = new ArrayList<Integer>();
String[] ss = s.trim().split(",");
for (int i = 0; i < ss.length; i++) {
String[] stemp = ss[i].trim().split("-");
menu.put(Integer.parseInt(stemp[0]),
stemp[1]); // key-menu
ids.add(Integer.parseInt(stemp[0])); // keylist
}
tvlist.setMenu(menu);
tvlist.setIds(ids);
}
} else if (name.equals("channel")) {
urlList=new ArrayList<String>();
element = new Channel();
element.setId(parser.getAttributeValue(null, "id")); // id
element.setclassnum(parser.getAttributeValue(null, "classnum"));
element.setUrl(parser
.getAttributeValue(null, "src"));
element.setEpg(parser
.getAttributeValue(null, "epg"));
element.setIcon(parser.getAttributeValue(null,
"icon"));
element.setTitle(parser.getAttributeValue(null,
"name"));
element.setCid(parser
.getAttributeValue(null, "cid"));
element.setTn(Integer.parseInt(parser
.getAttributeValue(null, "tn")));
element.setType(parser.getAttributeValue(null,
"class"));
element.setHimg(parser.getAttributeValue(null,
"himg"));
}else if(name.equalsIgnoreCase("url")){
urlList.add(parser.nextText().trim());
}
}else if(eventType ==XmlPullParser.END_TAG){
if(parser.getName().equalsIgnoreCase("channel")){
element.setUrlList(urlList);
element.setTn(urlList.size());
channelList.add(element);
}
}
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;
} finally {
if (mHttpClient != null) {
mHttpClient.close();
}
}
tvlist.setChannels(channelList);
return tvlist;
}
return null;
}
}
相关文章推荐
- android中用XmlPullParser解析XML文件,以及res/raw和assets的不同
- XmlPullParser解析xml文件
- Android中采用XmlPullParser解析xml文件异常的问题
- XmlPullParser解析xml文件
- Pull(XmlPullParser)解析Xml文件
- Android学习第七天————通过android提供的XmlPullParserFactory解析XML文件
- android使用XmlPullParser来解析XML文件
- 安卓中使用XmlPullParser解析xml文件
- XmlPullParser 解析xml文件
- XmlPullParser 解析XMl文件
- android使用XmlPullParser来解析XML文件
- android利用HttpURLConnection+XmlPullParser解析Xml文件
- XmlPullParser解析xml文件(还原短信)
- Android开发学习---使用XmlPullParser解析xml文件
- android使用XmlPullParser来解析XML文件
- 用XmlPullParser解析XML文件
- Android 解析XML文件 XmlPullParser 方法
- Android编程解析XML文件的方法详解【基于XmlPullParser】
- Android_XmlPullParser解析xml文件_141014
- Android下Xml解析技术(四)、pull生成Xml文件