我正在尝试解析RSS Feed:http://feeds.feedburner.com/Talk_Android
我已经使用了在线教程并对其进行了修改。
我已成功检索到标题和pubDate。我也尝试检索描述标签,它已经工作..但我似乎无法弄清楚如何检索内容:编码标签。我想要检索标题和内容:编码的onclick项目listview ..标题出来很好..在调试时内容为null,这显然是因为我使用了字符串' content'在我的代码中没有出现在rss feed中的任何地方......我已经浏览了所有类似的帖子,但似乎无法清楚地了解我应该做的事情。做.. 任何帮助将不胜感激..
这是NewsBean.java
package com.samir;
public class NewsBean {
private String title;
private String description;
private String pubDate;
private String link;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getContent() {
return content + " got null here ";
}
public void setContent(String content) {
this.content = content;
}
}
以下是NewsMainActivity.java
package com.samir;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class NewsMainActivity extends Activity implements OnItemClickListener {
ListView listview;
List<NewsBean> arrayList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComponents();
if (Utils.isNetworkAvailable(this)) {
new MyRssReadTask()
.execute("http://feeds.feedburner.com/Talk_Android");
} else {
Toast.makeText(this, "No Network Connection!!!", Toast.LENGTH_SHORT)
.show();
}
}
private void initComponents() {
listview = (ListView) findViewById(android.R.id.list);
listview.setOnItemClickListener(this);
}
class MyRssReadTask extends AsyncTask<String, Void, Void> {
ProgressDialog waitingDialog;
@Override
protected void onPreExecute() {
waitingDialog = new ProgressDialog(NewsMainActivity.this);
waitingDialog.setMessage("Loading...");
waitingDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(String... urls) {
arrayList = new NewsParser().getData(urls[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
waitingDialog.dismiss();
setDataToListView();
super.onPostExecute(result);
}
}
protected void setDataToListView() {
if (null != arrayList && arrayList.size() != 0) {
NewsRowAdapter objNewsRowAdapter = new NewsRowAdapter(
NewsMainActivity.this, R.layout.row, arrayList);
listview.setAdapter(objNewsRowAdapter);
} else {
showToast("Error in geting Data From WebService!");
}
}
void showToast(String msg) {
Toast.makeText(NewsMainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// do description showing work here
final NewsBean objBean = (NewsBean) arrayList.get(position);
Intent intent = new Intent(NewsMainActivity.this,
NewsDetail.class);
intent.putExtra("title", objBean.getTitle());
intent.putExtra("description", objBean.getDescription());
intent.putExtra("content", objBean.getContent());
// String encodedurl = null;
// try {
// encodedurl = URLEncoder.encode(objBean.getDescription(),"UTF-8");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// intent.putExtra("content",encodedurl );
//
startActivity(intent);
}
} //NewsMain ends
以下是NewsDetail.java
package com.samir;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NewsDetail extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Bundle b = this.getIntent().getExtras();
String title = b.getString("title");
String desc = b.getString("description");
String cont = b.getString("content");
// String content = getIntent().getStringExtra("content");
TextView tvtitle = (TextView) findViewById(R.id.tvtitle);
TextView tvdesc = (TextView) findViewById(R.id.tvdesc);//USED FOR CONTENT RIGHT NOW
tvtitle.setText(title);
tvdesc.setText(cont);
// tvdesc.setText(content);
//tvdate.setText(Utils.getDateFormate(date));
setTitle(title);
}
}
提前谢谢!
答案 0 :(得分:0)
尝试使用此库。它应该对你完美无缺。 https://github.com/Pkmmte/PkRSS
此RSS库非常易于使用,并支持许多不同类型的Feed。默认情况下,它适用于RSS 2.0(这是您的Feed使用的),因此它应该更容易。
要在后台加载和解析您的Feed,代码如下所示: PkRSS.with(本).load(URL).async();
然后,您可以通过各种不同的方式检索结果,例如通过回调或简单地: 列出articleList = PkRSS.with(this).get(url);
访问GitHub页面了解更多详情。我使用这个库构建了一个科技新闻应用程序,它可以创造奇迹。 :d