问题
My app downloads titles from the news webpage and display them in a listview, kinda a RSSReader, now when i try to run it but it shows error.
Code:-
public class RSSReaderActivity extends ListActivity
{ List headlines, links;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DoSomeTask().execute();
}
// DoSomeTask class will do all the work on another thread so there will be no
// ANR and UI hanging.
private class DoSomeTask extends AsyncTask<Void, Void, Void>
{
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {
headlines = new ArrayList();
links = new ArrayList();
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void params)
{
try{
// Binding data
ArrayAdapter adapter = new ArrayAdapter(RSSReaderActivity.this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}catch (Exception e) {}
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
Logcat:-
06-23 19:37:57.505: W/dalvikvm(7450): threadid=8: thread exiting with uncaught exception (group=0x400207d8)
06-23 19:37:57.785: E/AndroidRuntime(7450): FATAL EXCEPTION: AsyncTask #1
06-23 19:37:57.785: E/AndroidRuntime(7450): java.lang.RuntimeException: An error occured while executing doInBackground()
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.lang.Thread.run(Thread.java:1096)
06-23 19:37:57.785: E/AndroidRuntime(7450): Caused by: java.lang.IllegalArgumentException
06-23 19:37:57.785: E/AndroidRuntime(7450): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1040)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:56)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:1)
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-23 19:37:57.785: E/AndroidRuntime(7450): ... 4 more
So yeah, the error is in doInBackground(), as i am new multi-thread programming, please point me out whats the problem with the code. Thanks.
回答1:
You have error in following line
xpp.setInput(getInputStream(url), "UTF_8");
Replace UTF_8
by UTF-8
Also remove setContentView(R.layout.main);
Below is snippt.
package org.sample;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SampleActivity extends ListActivity {
List headlines, links;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new DoSomeTask().execute();
}
// DoSomeTask class will do all the work on another thread so there will be
// no
// ANR and UI hanging.
private class DoSomeTask extends AsyncTask<Void, Void, Void> {
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {
headlines = new ArrayList();
links = new ArrayList();
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF-8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText()); // extract the
// headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); // extract the link
// of article
}
} else if (eventType == XmlPullParser.END_TAG
&& xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next(); // move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void params) {
try {
// Binding data
ArrayAdapter adapter = new ArrayAdapter(AbcActivity.this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
} catch (Exception e) {
}
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
回答2:
More than likely the problem is somewhere in this code:
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
From the source code of KXmlParser:
if (is == null)
throw new IllegalArgumentException();
What is happening is an exception is thrown in your call to url.openConnection().getInputStream();
causing the method to return null, and XMLPullParser's setInput
in turn is throwing an InvalidArgumentException as seen from its source. Try printing the stacktrace in the catch block to the console (Log.e("some_tag", "some message", e);
before returning null and see what the exception is, and go from there.
Also, to solve the "UTF_8" vs "UTF-8" debate the other poster brings up, simply use xpp.setInput(getInputStream(url), HTTP.UTF_8);
回答3:
Phew...atlast its fixed.
I just changed
xpp.setInput(getInputStream(url), "UTF_8");
to
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
and Viola :)
来源:https://stackoverflow.com/questions/11170151/execute-doinbackground-showing-illegalargumentexception