我正在尝试申请阅读 rss 提要。我制作了应用程序,但它不会加载提要。我会给你代码,这样你就可以告诉我我可以改变什么来使应用程序正常工作。我不知道问题是什么。代码中没有错误,但它不会加载提要。
ListActivity.java:
package com.listview;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ListActivity extends Activity {
ListView lv1;
ProgressDialog ShowProgress;
public ArrayList<Post> PostList = new ArrayList<Post>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv1 = (ListView)findViewById(R.id.listView1);
ShowProgress = ProgressDialog.show(ListActivity.this,"","Loading. Please wait...",true);
new loadingTask().execute("av-gurus.blogspot.com/feeds/posts/default/");
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,
int position, long id){
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse(PostList.get(position).getUrl()));
startActivity(intent);
}
});
}
class loadingTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
SAXHelper sh = null;
try
{
sh = new SAXHelper(urls[0]);
}catch (MalformedURLException e){
e.printStackTrace();
}
sh.parseContent("");
return "";
}
protected void onPostExecute(String s) {
lv1.setAdapter(new EfficientAdapter(ListActivity.this, PostList));
ShowProgress.dismiss();
}
}
class SAXHelper{
public HashMap<String, String> userList = new HashMap<String, String>();
private URL url2;
public SAXHelper(String url1) throws MalformedURLException {
this.url2 = new URL(url1);
}
public RSSHandler parseContent(String parseContent) {
RSSHandler df = new RSSHandler();
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(df);
xr.parse(new InputSource(url2.openStream()));
} catch (Exception e) {
e.printStackTrace();
}
return df;
}
}
class RSSHandler extends DefaultHandler {
private Post currentPost = new Post();
StringBuffer chars = new StringBuffer();
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("item")) {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("title")
&& currentPost.getTitle() == null) {
currentPost.setTitle(chars.toString());
}
if (localName.equalsIgnoreCase("pubDate")
&& currentPost.getPubDate() == null) {
currentPost.setPubDate(chars.toString());
}
if (localName.equalsIgnoreCase("thumbnail")
&& currentPost.getThumbnail() == null) {
currentPost.setThumbnail(chars.toString());
}
if (localName.equalsIgnoreCase("link")
&& currentPost.getUrl() == null) {
currentPost.setUrl(chars.toString());
}
if (localName.equalsIgnoreCase("item")) {
PostList.add(currentPost);
currentPost = new Post();
}
}
@Override
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
}
}
Post.java:
package com.listview;
public class Post {
private String title;
private String thumbnail;
private String url;
private String description;
private String pubDate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getPubDate() {
return pubDate;
}
}
高效适配器.java:
package com.listview;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Post> data;
private static LayoutInflater inflater = null;
//public ImageLoader imageLoader;
ViewHolder holder;
EfficientAdapter(Activity a, ArrayList<Post> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
return data.toArray().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView label;
public TextView addr;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.title);
holder.addr = (TextView) vi.findViewById(R.id.details);
holder.image = (ImageView) vi.findViewById(R.id.thumb);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.label.setText(data.get(position).getTitle());
holder.addr.setText(data.get(position).getPubDate());
imageLoader.DisplayImage((data.get(position).getThumbnail()), activity,
holder.image, 72, 72);
URL url = null;
try {
url = new URL((data.get(position).getThumbnail()));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream content = null;
try {
content = (InputStream)url.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(content , "src");
Bitmap mIcon1 = null;
try {
mIcon1 =
BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false));
return vi;
}
}
活动列表.xml:
<LinearLayout 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"
tools:context=".ListActivity" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:id="@+id/listView1">
</ListView>
</LinearLayout>
行.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget30"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="4dip" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/details" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="TextView"
android:textColor="#000000" android:layout_toRightOf="@+id/icon"
android:layout_alignLeft="@+id/title" android:layout_alignRight="@+id/title"
android:layout_below="@+id/title" android:layout_marginRight="5px">
</TextView>
<TextView android:id="@+id/title" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="TextView"
android:textStyle="bold" android:textColor="#000000"
android:layout_marginTop="5px" android:layout_toRightOf="@+id/icon"
android:layout_marginRight="5px">
</TextView>
<ImageView android:id="@+id/arrow" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="0px"
android:layout_marginRight="10px" android:src="@drawable/ic_launcher"
android:layout_alignRight="@+id/icon" android:layout_alignParentRight="true"
android:layout_centerVertical="true">
</ImageView>
<ImageView android:id="@+id/thumb" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="10px"
android:layout_centerVertical="true" android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" android:minWidth="72dip"
android:minHeight="72dip" android:maxWidth="72dip" android:maxHeight="72dip">
</ImageView>
</RelativeLayout>