80,475
社区成员
发帖
与我相关
我的任务
分享 UpdateInfoParser updateInfoParser=new UpdateInfoParser();
List<msgContent> b=updateInfoParser.parse(input);
我这样调用,想获取list中的值,总是报空,大神有办法么public class msgContent {
private String appVersion;
private String latestAppVersion;
private String latestAppVersionUrl;
private String appType;
private String appFlag;
private String responseCode;
public msgContent() {
}
public msgContent(String appVersion,String latestAppVersion, String latestAppVersionUrl,
String appType,String appFlag,String responseCode){
this.appFlag=appFlag;
this.appType=appType;
this.appVersion=appVersion;
this.latestAppVersion=latestAppVersion;
this.latestAppVersionUrl=latestAppVersionUrl;
this.responseCode=responseCode;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
public String getLatestAppVersion() {
return latestAppVersion;
}
public void setLatestAppVersion(String latestAppVersion) {
this.latestAppVersion = latestAppVersion;
}
public String getLatestAppVersionUrl() {
return latestAppVersionUrl;
}
public void setLatestAppVersionUrl(String latestAppVersionUrl) {
this.latestAppVersionUrl = latestAppVersionUrl;
}
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
public String getAppFlag() {
return appFlag;
}
public void setAppFlag(String appFlag) {
this.appFlag = appFlag;
}
}
public class UpdateInfoParser extends Activity implements MessageParse { @Override
public List<msgContent> parse(InputStream inStream) throws Exception {
List<msgContent> updateinfolist=null;
msgContent updateinfo=null;
XmlPullParser xmlPullParser= Xml.newPullParser();
xmlPullParser.setInput(inStream,"UTF-8");
int type=xmlPullParser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_DOCUMENT:
{
updateinfolist =new ArrayList<msgContent>();
}
break;
case XmlPullParser.START_TAG:
String tag=xmlPullParser.getName();
updateinfo=new msgContent();
if(tag.equalsIgnoreCase("latesAppVersion")){
updateinfo.setAppType(xmlPullParser.nextText());
}else if(tag.equalsIgnoreCase("appVersion")){
updateinfo.setAppVersion(xmlPullParser.nextText());
}if(tag.equalsIgnoreCase("responseCode")){
updateinfo.setResponseCode(xmlPullParser.nextText());
}if(tag.equalsIgnoreCase("appFlag")){
updateinfo.setAppFlag(xmlPullParser.nextText());
}if(tag.equalsIgnoreCase("appType")){
updateinfo.setAppType(xmlPullParser.nextText());
}if(tag.equalsIgnoreCase("latestAppVersionUrl")){
updateinfo.setLatestAppVersionUrl(xmlPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if(xmlPullParser.getName().equalsIgnoreCase("appInfo")){
updateinfolist.add(updateinfo);
updateinfo=null;
}
}
type=xmlPullParser.next();
}
return updateinfolist;
}
}
public void xml(int id) throws XmlPullParserException, IOException{
String str = "";
XmlPullParser xmlPullParser= Xml.newPullParser();
xmlPullParser.setInput(getResources().openRawResource(id),"UTF-8");就这样吧他需要的参数传递进去就okpublic List<msgContent> parse(String result){}
就像这样的,下面那个setInput又怎么办呢??
我是想把这个方法封装起来,然后再别处调用它
谢谢你啊先给分给你 public void xml() throws XmlPullParserException, IOException{
String str = "";
XmlPullParser xmlPullParser= Xml.newPullParser();
xmlPullParser.setInput(getResources().openRawResource(R.raw.file),"UTF-8");
int type=xmlPullParser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_DOCUMENT:
{
// updateinfolist =new ArrayList<msgContent>();
}
break;
case XmlPullParser.START_TAG:
String tag=xmlPullParser.getName();
// if(tag.equalsIgnoreCase("appInfo")){
// updateinfo=new msgContent();
// statrtappInfo=true;
// }
if(tag.equalsIgnoreCase("latesAppVersion")){
// updateinfo.setLatestAppVersion(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}else if(tag.equalsIgnoreCase("appVersion")){
// updateinfo.setAppVersion(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}if(tag.equalsIgnoreCase("responseCode")){
// updateinfo.setResponseCode(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}if(tag.equalsIgnoreCase("appFlag")){
// updateinfo.setAppFlag(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}if(tag.equalsIgnoreCase("appType")){
// updateinfo.setAppType(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}if(tag.equalsIgnoreCase("latestAppVersionUrl")){
// updateinfo.setLatestAppVersionUrl(xmlPullParser.getText());
str +="=====" + xmlPullParser.nextText()+";" ;
}
break;
case XmlPullParser.END_TAG:
if(xmlPullParser.getName().equals("appInfo")){
// updateinfolist.add(updateinfo);
// updateinfo=null;
}
}
type=xmlPullParser.next();
}
System.out.print(str);
}
成功拿到数据package com.bill99.kuaiqian;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class UpdateInfoParser implements MessageParse{
private Boolean statrtappInfo=false;
@Override
public List<msgContent> parse(InputStream inStream) throws Exception {
List<msgContent> updateinfolist=null;
msgContent updateinfo=null;
XmlPullParser xmlPullParser= Xml.newPullParser();
xmlPullParser.setInput(inStream,"UTF-8");
int type=xmlPullParser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_DOCUMENT:
{
updateinfolist =new ArrayList<msgContent>();
}
break;
case XmlPullParser.START_TAG:
String tag=xmlPullParser.getName();
if(tag.equalsIgnoreCase("appInfo")){
updateinfo=new msgContent();
type=xmlPullParser.next();
statrtappInfo=true;
}
if(tag.equalsIgnoreCase("latesAppVersion")){
updateinfo.setLatestAppVersion(xmlPullParser.getText());
type=xmlPullParser.next();
}else if(tag.equalsIgnoreCase("appVersion")){
updateinfo.setAppVersion(xmlPullParser.getText());
type=xmlPullParser.next();
}if(tag.equalsIgnoreCase("responseCode")){
updateinfo.setResponseCode(xmlPullParser.getText());
type=xmlPullParser.next();
}if(tag.equalsIgnoreCase("appFlag")){
updateinfo.setAppFlag(xmlPullParser.getText());
type=xmlPullParser.next();
}if(tag.equalsIgnoreCase("appType")){
updateinfo.setAppType(xmlPullParser.getText());
type=xmlPullParser.next();
}if(tag.equalsIgnoreCase("latestAppVersionUrl")){
updateinfo.setLatestAppVersionUrl(xmlPullParser.getText());
type=xmlPullParser.next();
}
break;
case XmlPullParser.END_TAG:
if(xmlPullParser.getName().equals("appInfo")){
updateinfolist.add(updateinfo);
updateinfo=null;
}
}
type=xmlPullParser.next();
}
return updateinfolist;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);