我尝试使用jsoup从网站解析新闻标题并将其显示在ListView中。我一直试图解决这个问题很长一段时间,并用Google搜索疯狂,但我无法解决我的问题或找到一个有效的解决方案。我有一个自定义类,它包含新闻标题和文章链接两个变量。似乎一切都解析得很好,但我只是无法正确地显示我的ListView或者根本不能......它会不断崩溃,并且每当我遇到不同的错误时它就会连接起来。也许我对自己太难了。我很沮丧,不能再逻辑思考......我真的很感激任何提示或有用的答案。
Feeds class:
public class Feeds {
private String mNewsTitle;
private String mNewsLink;
public Feeds(String newsTitle, String newsLink){
mNewsTitle = newsTitle;
mNewsLink = newsLink;
}
public String getNewsTitle(){
return mNewsTitle;
}
public void setNewsTitle(String newsTitle){
mNewsTitle = newsTitle;
}
public String getNewsLink(){
return mNewsLink;
}
public void setNewsLink(String newsLink){
mNewsTitle = newsLink;
}
}
NewsFeeds类:
public class NewsFeeds extends ListActivity {
private ArrayList<Feeds> mFeedDB = new ArrayList<Feeds>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feeds);
HtmlParser htmlThread = new HtmlParser();
htmlThread.execute();
} // end on create
public class HtmlParser extends AsyncTask<Void, Integer, ArrayList<Feeds>> {
private static final int NETWORK_NO_ERROR = -1;
private static final int NETWORK_HOST_UNREACHABLE = 1;
private static final int NETWORK_NO_ACCESS_TO_INTERNET = 2;
private static final int NETWORK_TIME_OUT = 3;
Integer serverError = NETWORK_NO_ERROR;
ProgressDialog dialog;
protected void onPreExecute() {
// example of setting up something
dialog = new ProgressDialog(NewsFeeds.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Retrieving News Feeds");
dialog.show();
} // end onPreExecute
@Override
protected ArrayList<Feeds> doInBackground(Void... params) {
try {
// need http protocol
Document doc = Jsoup.connect("http://baseball-potsdam.de/news")
.get();
// get news feed titles
Elements newsFeed = doc.getElementsByClass("gdlr-blog-title");
// get all links
Elements links = newsFeed.select("a[href]");
for (Element link : links) {
// populate ArrayList with news titles and links
mFeedDB.add(new Feeds(link.text(), link.attr("href")));
}
return mFeedDB;
// } catch (IOException e) {
// e.printStackTrace();
} catch (ConnectException e) {
serverError = NETWORK_NO_ACCESS_TO_INTERNET;
return null;
} catch (UnknownHostException e) {
serverError = NETWORK_HOST_UNREACHABLE;
return null;
} catch (SocketTimeoutException e) {
serverError = NETWORK_TIME_OUT;
return null;
} catch (IOException e) {
e.printStackTrace();
} // end try catch
return null;
} // end doInBackground
protected void onProgressUpdate(Integer... progress) {
} // end onProgressUpdate
protected void onPostExecute(ArrayList<Feeds> result) {
if (result != null) {
ListView listview = (ListView) findViewById(R.id.list_view_news_feeds);
listview.setAdapter(new ArrayAdapter<Feeds>(NewsFeeds.this, android.R.layout.simple_list_item_1 , mFeedDB));
if (dialog.isShowing()) {
dialog.dismiss();
} // end if
} else {
switch (serverError) {
case NETWORK_NO_ERROR:
Toast.makeText(NewsFeeds.this,
"Probably, invalid response from server",
Toast.LENGTH_LONG).show();
break;
case NETWORK_NO_ACCESS_TO_INTERNET:
// You can customize error message (or behavior) for
// different type of error
case NETWORK_TIME_OUT:
case NETWORK_HOST_UNREACHABLE:
Toast.makeText(NewsFeeds.this, "Error in Connection",
Toast.LENGTH_LONG).show();
break;
}
} // end if else
} // end onPostExecute
} // end HtmlParser class
} // end NewsFeeds
activity_news_feeds.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_view_news_feeds"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0.1dp"
android:divider="#0000CC"
/>
</LinearLayout>
NewsManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kylehopeman.android.porcupinesnews"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.kylehopeman.android.porcupinesnews.MainMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.kylehopeman.android.porcupinesnews.NewsFeeds"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.kylehopeman.android.porcupinesnews.NewsFeeds" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
如果您收到ListView的错误,它看起来只会在postExecute块中实例化。是否可以在onCreate()中实例化它并将其声明为声明mFeedDB的位置?
答案 1 :(得分:0)
在我的NewsFeeds.java文件中更改第一行后:
public class NewsFeeds extends ListActivity
为:
public class NewsFeeds extends Activity
错误消失了,应用程序编译和工作就像我想要的那样。