我的代码从网站获取RSS提要并在列表视图中显示。我已编写代码,但显示空白页面。
MainActivity.java
package com.example.rssreadertest;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button bt;
EditText txt;
RSSfeed feed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
txt = (EditText) findViewById(R.id.editText1);
bt.setOnClickListener(this);
Log.d("check", "onCreate was called");
}
public void onClick(View v)
{
int id=v.getId();
switch(id)
{
case R.id.button1:
Log.d("check", "Button was clicked");
String feedUrl = "http://www.websiteurl.com/feed/";
new AsyncLoadXMLFeed(feedUrl).execute();
Log.d("check", "url" +feedUrl +" was sent");
}
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>
{
String feedUrl;
public AsyncLoadXMLFeed(String feedUrl) {
this.feedUrl = feedUrl;
}
@Override
protected Void doInBackground(Void... arg0) {
DOMParser parser = new DOMParser();
feed = parser.read(feedUrl);
Log.d("check", "parsing begins");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
Log.d("check", "here s the feed " +feed);
}
}
}
ListActivity.java
package com.example.rssreadertest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity {
RSSfeed feed;
CustomAdapter ca;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_feed);
Log.d("check", "entered");
feed = (RSSfeed) getIntent().getExtras().get("feed");
lv = (ListView) findViewById(R.id.listView1);
ca = new CustomAdapter(this);
lv.setAdapter(ca);
}
class CustomAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public CustomAdapter(ListActivity listActivity) {
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater) listActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return feed.getItemcount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View listItem = arg1;
int pos = arg0;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
TextView tvTitle = (TextView) listItem.findViewById(R.id.textView1);
TextView tcDesc = (TextView) listItem.findViewById(R.id.textView2);
Log.d("check", feed.getItem(pos).getTitle());
Log.d("check", feed.getItem(pos).getDesc());
tvTitle.setText(feed.getItem(pos).getTitle());
tcDesc.setText(feed.getItem(pos).getDesc());
return listItem;
}
}
}
DOMParser.java
package com.example.rssreadertest;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.util.Log;
public class DOMParser {
RSSfeed feed = new RSSfeed();
RSSItem item = new RSSItem();
public RSSfeed read(String link)
{
URL url = null;
try {
url = new URL(link);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
try {
// Create required instances
DocumentBuilderFactory dbf;
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the xml
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Get all <item> tags.
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();
for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i);
NodeList nchild = currentNode.getChildNodes();
int clength = nchild.getLength();
// Get the required elements from each Item
for (int j = 0; j < clength; j = j++) {
Node thisNode = nchild.item(j);
String theString = null;
String nodeName = thisNode.getNodeName();
theString = nchild.item(j).getFirstChild().getNodeValue();
if (theString != null) {
if ("title".equals(nodeName)) {
// Node name is equals to 'title' so set the Node
// value to the Title in the RSSItem.
item.setTitle(theString);
Log.d("check", "Title is " +theString);
}
else if ("description".equals(nodeName)) {
item.setDesc(theString);
Log.d("check", "desc is " +theString);
}
}
}
// add item to the list
feed.addItem(item);
}
}
catch (Exception e)
{
}
return feed;
}
}
RSSfeed.java
package com.example.rssreadertest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class RSSfeed implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<RSSItem> itemlist = new ArrayList<RSSItem>();
private int itemcount=0;
public List<RSSItem> getItemlist() {
return itemlist;
}
public void addItem(RSSItem item)
{
itemlist.add(item);
itemcount++;
}
public int getItemcount() {
return itemcount;
}
public RSSItem getItem(int arg0) {
// TODO Auto-generated method stub
return itemlist.get(arg0);
}
}
RSSitem.java
public class RSSItem {
private String title,desc;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
布局文件:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rssreadertest.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp"
android:text="Start to parse" />
</RelativeLayout>
list_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="31dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="35dp" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textColor="#FC0707"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
我无法找到解决方案,因为它没有显示任何错误。任何帮助都会有用。
输出: