<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?> |
|
这是从服务器返回的数据流。。。直接解析数据流
|
|
这个自己百度一下有很多的 直接给代码的话没人时间帮你去做,而且自己做的话效果更好,你可以在做的过程中把遇到的不能解决问题发出来
|
|
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; } } |
|
已经做了,但是最后输出的结果是空值,不知道啥情况
|
|
不要直接发代码 用代码格式发 不然看不清楚 例如:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); |
|
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; } } |
|
type=xmlPullParser.next(); 除了 留下58行的 全部干掉 |
|
赋值不要使用gettext() 使用xmlPullParser.nextText();
|
|
求大神啊。。。。怎么解决
|
|
50分 |
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); } 成功拿到数据 |
大神,如果这个方法有参数呢
public List<msgContent> parse(String result){} 就像这样的,下面那个setInput又怎么办呢?? 谢谢你啊先给分给你 |
|
50分 |
public void xml(int id) throws XmlPullParserException, IOException{ String str = ""; XmlPullParser xmlPullParser= Xml.newPullParser(); xmlPullParser.setInput(getResources().openRawResource(id),"UTF-8"); 就这样吧他需要的参数传递进去就ok |
还有个问题啊,比如我有个字符串result,在主mainactivity中调用这个方法的时候怎么调用啊
|
|
你好 问题解决了么 不好意思 我下班一般不上CSDN的 |
|
没事没事。问题没解决呢,按照你的方法,里面的是方法参数是int类型啊,可是我调用这个方法处理的是数据流或者string字符串,这个该如何解决呢,后来按照我之前的写的,存入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; } } |
|
现在我主函数有个input数据流,或者一个string字符串想调用这个方法,应该怎么调用??
|
|
UpdateInfoParser updateInfoParser=new UpdateInfoParser(); List<msgContent> b=updateInfoParser.parse(input); 我这样调用,想获取list中的值,总是报空,大神有办法么 |
|
汗 刚才有点忙 没时间上csdn 现在才看到你消息,你加我扣 扣吧 469534249 |