我正在尝试从网站读取 RSS 提要并将它们显示在我的 android 应用程序中。但是,我遇到了错误。有任何想法吗?我将在代码之后发布我的 logcat。请帮忙。
package nidhin.rss;
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.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class RssActivity extends Activity {
/** Called when the activity is first created. */
Button ButtonStats ;
List headlines;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonStats = (Button) findViewById(R.id.ButtonStats);
ButtonStats.setOnClickListener(new clicker());
headlines = new ArrayList();
try {
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
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(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();
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
if(v== ButtonStats)
{
}
}
}
}
日志猫:
08-02 21:40:02.320: E/AndroidRuntime(723): FATAL EXCEPTION: main
08-02 21:40:02.320: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhin.rss/nidhin.rss.RssActivity}: android.os.NetworkOnMainThreadException
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.os.Looper.loop(Looper.java:137)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-02 21:40:02.320: E/AndroidRuntime(723): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 21:40:02.320: E/AndroidRuntime(723): at java.lang.reflect.Method.invoke(Method.java:511)
08-02 21:40:02.320: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-02 21:40:02.320: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-02 21:40:02.320: E/AndroidRuntime(723): at dalvik.system.NativeStart.main(Native Method)
08-02 21:40:02.320: E/AndroidRuntime(723): Caused by: android.os.NetworkOnMainThreadException
08-02 21:40:02.320: E/AndroidRuntime(723): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-02 21:40:02.320: E/AndroidRuntime(723): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-02 21:40:02.320: E/AndroidRuntime(723): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-02 21:40:02.320: E/AndroidRuntime(723): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-02 21:40:02.320: E/AndroidRuntime(723): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-02 21:40:02.320: E/AndroidRuntime(723): at nidhin.rss.RssActivity.getInputStream(RssActivity.java:107)
08-02 21:40:02.320: E/AndroidRuntime(723): at nidhin.rss.RssActivity.onCreate(RssActivity.java:47)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.Activity.performCreate(Activity.java:4465)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-02 21:40:02.320: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-02 21:40:02.320: E/AndroidRuntime(723): ... 11 more