gpt4 book ai didi

java - 如何在 Android 的 ListView 中显示解析后的 html

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:50 26 4
gpt4 key购买 nike

我正在尝试使用 jsoup 解析网站上的新闻标题并将其显示在 ListView 中。我已经尝试解决这个问题很长时间了,并且疯狂地用谷歌搜索,但我无法解决我的问题或找到可行的解决方案。我有一个自定义类,其中包含两个变量:新闻标题和文章链接。似乎一切都解析得很好,但我就是无法让我的 ListView 正确显示或根本无法显示......它不断崩溃,并且每次我收到不同的错误时都会出现这种情况。也许我对自己太难了。我很沮丧,无法再进行逻辑思考...我真的很感激任何和所有提示或有用的答案。

Feed 类:

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>

最佳答案

如果您在使用 ListView 时遇到错误,它看起来只会在 postExecute block 中实例化。是否可以在 onCreate() 中实例化它并在声明 mFeedDB 的位置声明它?

关于java - 如何在 Android 的 ListView 中显示解析后的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986278/

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