Last active
August 29, 2015 14:13
-
-
Save AndrewWang1993/c686345517cfe0f4029d to your computer and use it in GitHub Desktop.
XML Media Parser(视频播放列表XML解析)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://121.40.186.170/shipin/test1.php | |
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() | |
.detectDiskReads().detectDiskWrites().detectNetwork() | |
.penaltyLog().build()); | |
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() | |
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects() | |
.penaltyLog().penaltyDeath().build()); | |
XmlPullParser parser = null; | |
try { | |
InputStream is = getInput("http://121.40.186.170/shipin/test1.php"); | |
XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance(); | |
XmlPullParser xmlPullParser = pullFactory.newPullParser(); | |
xmlPullParser.setInput(is, "UTF-8"); | |
provinceandcityParser = xmlPullParser; | |
} catch (Exception e) { | |
Log.v("TAG", e.toString()); | |
} | |
ArrayList<Object> arrarys = MediaParser.ParseXml(provinceandcityParser); | |
Log.v("TAG", arrarys.toString()); | |
HashMap<String, String[]> mURL = new ProcessResult().process(arrarys); | |
Log.v("TAG", mURL.toString()); | |
private InputStream getInput(String url) { | |
URL infoUrl = null; | |
InputStream inStream = null; | |
try { | |
infoUrl = new URL(url); | |
URLConnection connection = infoUrl.openConnection(); | |
HttpURLConnection httpConnection = (HttpURLConnection) connection; | |
inStream = httpConnection.getInputStream(); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return inStream; | |
} | |
public class MediaParser { | |
public static ArrayList<Object> ParseXml(XmlPullParser parser) { | |
ArrayList<Object> arrarys = new ArrayList<Object>(); | |
ArrayList<Object> content =new ArrayList<Object>(); | |
ArrayList<Pic> picArrary = new ArrayList<Pic>(); | |
Meg meg = null; | |
String type; | |
String videoUrl; | |
String webUrl; | |
String descri; | |
Pic pic = null; | |
String picUrl; | |
String name; | |
String id; | |
try { | |
int eventType = parser.getEventType(); | |
while (eventType != XmlPullParser.END_DOCUMENT) { | |
switch (eventType) { | |
case XmlPullParser.START_DOCUMENT: | |
break; | |
case XmlPullParser.START_TAG: | |
String tagNameStart = parser.getName(); | |
if (tagNameStart.equals("Message")) { | |
content =new ArrayList<Object>(); | |
meg = new Meg(); | |
parser.next(); | |
parser.next(); | |
type=parser.nextText(); | |
parser.next(); | |
parser.next(); | |
videoUrl = parser.nextText(); | |
parser.next(); | |
parser.next(); | |
webUrl = parser.nextText(); | |
parser.next(); | |
parser.next(); | |
descri = parser.nextText(); | |
meg.setType(type); | |
meg.setVideoUrl(videoUrl); | |
meg.setWebUrl(webUrl); | |
meg.setDescri(descri); | |
} else if (tagNameStart.equals("Pics")) { | |
picArrary = new ArrayList<Pic>(); | |
} else if (tagNameStart.equals("Pic")) { | |
parser.next(); | |
parser.next(); | |
parser.next(); | |
picUrl = parser.nextText(); | |
parser.next(); | |
name = parser.nextText(); | |
parser.next(); | |
id = parser.nextText(); | |
pic = new Pic(); | |
pic.setPicUrl(picUrl); | |
pic.setName(name); | |
pic.setId(id); | |
} | |
break; | |
case XmlPullParser.END_TAG: | |
String tagNameEnd = parser.getName(); | |
if (tagNameEnd.equals("Pic")) { | |
picArrary.add(pic); | |
} else if (tagNameEnd.equals("Pics")) { | |
} else if (tagNameEnd.equals("Message")) { | |
content.add(meg); | |
content.add(picArrary); | |
arrarys.add(content); | |
} | |
break; | |
case XmlPullParser.END_DOCUMENT: | |
break; | |
} | |
eventType = parser.next(); | |
} | |
} catch (XmlPullParserException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return arrarys; | |
} | |
} | |
public class ProcessResult { | |
public ArrayList<HashMap<String,String[]>> process(ArrayList<Object> arrary) { | |
ArrayList<HashMap<String,String[]>> arrayListHashMap=new ArrayList<HashMap<String, String[]>>(); | |
HashMap<String,String[]> hashMap; | |
int lenght = arrary.size(); | |
for (int i = 0; i < lenght; i++) { | |
ArrayList<Object> content; | |
content = (ArrayList<Object>)arrary.get(i); | |
Meg meg=(Meg)content.get(0); | |
ArrayList<Pic> picArrary=(ArrayList<Pic>)content.get(1); | |
hashMap=new HashMap<String, String[]>(); | |
if(meg.getType().equals("video")){ | |
String []strs={meg.getDescri(),picArrary.get(0).getPicUrl(),meg.getVideoUrl(),}; // {video:{desc,picurl,videourl}} | |
hashMap.put("video",strs); | |
}else if(meg.getType().equals("pic")){ | |
int picLength=picArrary.size()+1; | |
String strs[]=new String[picLength]; | |
strs[0]=meg.getDescri(); | |
for(int j=1;j<picLength;j++){ | |
Pic pic=picArrary.get(j-1); | |
strs[j]=pic.getPicUrl(); | |
} | |
hashMap.put("pics",strs); | |
}else if(meg.getType().equals("web")){ /// {web:{desc,picurl,videourl}} | |
String []strs={meg.getDescri(),picArrary.get(0).getPicUrl(),meg.getWebUrl(),}; | |
hashMap.put("web",strs); | |
} | |
arrayListHashMap.add(hashMap); | |
} | |
return arrayListHashMap; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment