gpt4 book ai didi

java - 在 Android 中解析 XML (XmlPullParserException)

转载 作者:行者123 更新时间:2023-12-01 11:00:08 26 4
gpt4 key购买 nike

我目前正在学习 Android 编程,当我尝试解析 XML 时,出现以下错误(程序运行正常,但仅解析第一个 XML 链接):

enter image description here

这是我的代码:

public class RSSActivity extends AppCompatActivity {

/**
* List of feeds that has been fetched.
*/
public static ArrayList<RSSFeed> Feeds;

/**
* Button for Seattle Times
*/
private Button mSeattleBtn;

/**
* Button for ESPN
*/
private Button mESPNBtn;

/**
* The layout contains the loading image
*/
private RelativeLayout mProgress;

/**
* {@inheritDoc}
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss);

Feeds = new ArrayList<>();
mProgress = (RelativeLayout) findViewById(R.id.loading);
mProgress.setVisibility(View.GONE);

mSeattleBtn = (Button) findViewById(R.id.seattle_times);
mSeattleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadXML().execute("http://www.seattletimes.com/feed/",
"http://www.seattletimes.com/seattle-news/feed/",
"http://www.seattletimes.com/nation-world/feed/");
}
});

mESPNBtn = (Button) findViewById(R.id.espn_btn);
mESPNBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadXML().execute("http://sports.espn.go.com/espn/rss/news",
"http://sports.espn.go.com/espn/rss/nfl/news",
"http://sports.espn.go.com/espn/rss/nba/news");
}
});

}

/**
* Async task to fetch the XML from the internet
*/
private class DownloadXML extends AsyncTask<String, Void, String> {

/**
* The name of the current class, used for Log (debugging)
*/
private static final String TAG = "DownloadXML";

/**
* The content of the xml that has been fetched from the internet
*/
private String xmlContent;

@Override
protected void onPreExecute() {
super.onPreExecute();
xmlContent = "";
mSeattleBtn.setVisibility(View.GONE);
mESPNBtn.setVisibility(View.GONE);
mProgress.setVisibility(View.VISIBLE);
}

/**
* {@inheritDoc}
*/
@Override
protected String doInBackground(String... params) {

// set the xml contents to xmlContent
//xmlContent = getXMLContent(params[0]);
for (String s: params) {
xmlContent += getXMLContent(s);
}

// This will return the xmlContent to the onPostExecute method.
return xmlContent;
}

/**
* Perform the actual downloading process of the RSS
* file here.
*
* @param path the url path of the rss feed.
* @return the completed download xml file (converted to String)
*/
private String getXMLContent(String path) {
StringBuilder temp = new StringBuilder();

try {

// Open the connection
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream inputStream = con.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

int charToRead;
// reading 1000 bytes at a time.
char[] input = new char[1000];

// Keep reading the file until there's no more bytes(chars) left to read
while(true) {
charToRead = inputStreamReader.read(input);
if(charToRead <= 0) {
break;
}
temp.append(String.copyValueOf(input, 0, charToRead));
}

return temp.toString();

} catch (IOException e) {
Log.d(TAG, "Error: " + e.getMessage());
}

return null;
}

/**
* {@inheritDoc}
*/
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
RSSFeed curFeed = null;
boolean inItem = false;
String value = "";

try {
// Instantiate XmlPullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// specify that the code will be supported by XML namespaces
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(result));
int event = parser.getEventType();

// Parse the XML content
while(event != XmlPullParser.END_DOCUMENT) {
String tag = parser.getName();

// Only parse with the starting and ending tag of "item"
// and every tags inside it, ignore other tags.
switch(event) {
case XmlPullParser.START_TAG:
// if the begin tag is item which mean
// we can begin to to fetch the xml tags we want into our application
if(tag.equalsIgnoreCase("item")) {
inItem = true;
curFeed = new RSSFeed();
}
break;
case XmlPullParser.TEXT:
value = parser.getText();
break;
case XmlPullParser.END_TAG:
/*
while reach the end tag of the current tag
if the end tag is title then set it to the current feed title,
if the end tag is link then set it to the current feed link,
if the end tag is pubdate then set it to the current feed pubdate,
if the end tag is item we know that there's no more contents to add
to the current feed so we move on to parse another feed.
*/
if(inItem){
if (tag.equalsIgnoreCase("title")) {
curFeed.setTitle(value);
} else if (tag.equalsIgnoreCase("link")) {
curFeed.setLink(value);
} else if (tag.equalsIgnoreCase("pubdate")) {
curFeed.setDate(value);
} else if (tag.equalsIgnoreCase("item")) {
Feeds.add(curFeed);
inItem = false;
}
}
break;
default:

}

event = parser.next();
}

} catch (Exception e) {
e.printStackTrace();
}

findViewById(R.id.loading).setVisibility(View.GONE);
Intent intent = new Intent(RSSActivity.this, FeedListActivity.class);
startActivity(intent);
finish();

}
}
}

由于我对所有这些都是新手,有人可以向我解释一下这是怎么回事吗?看起来错误来自解析器。

编辑:如果我只将一个参数传递给 Asynctask.execute() 那么一切都会正常运行。

解决方案:

现在唯一的解决方案是按 nikhil.thakkar 回复的顺序处理每个 URL

Then i would suggest you to process each url serially..

最佳答案

请检查 onPostExecute 方法。您正在访问 XML 文件中不存在的 token 。

关于java - 在 Android 中解析 XML (XmlPullParserException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com