Java (android) - RSS 解析到 ListView,错误?? (Android 发展)
Java (android) - RSS Parse to ListView, Error?? (Android Development)
我是 android 开发的新手(初学者),我在 Blogger.com 上有博客,想获取 RSS 数据并将其解析为 Android ListView,当我使用时,例如 https://www.sammobile.com/feeds/ it works, but when I change url adress to my blog rss adress: https://mystat115.blogspot.com/feeds/posts/default 它什么都没有..只是空白 activity...
这是我也用过的代码...
Article.Java
public class Article {
String title,description;
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;
}
}
Connector.Java
public class Connector {
public static Object connect(String urlAddress)
{
try
{
URL url=new URL(urlAddress);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
con.setDoInput(true);
return con;
} catch (MalformedURLException e) {
e.printStackTrace();
return ErrorTracker.WRONG_URL_FORMAT;
} catch (IOException e) {
e.printStackTrace();
return ErrorTracker.CONNECTION_ERROR;
}
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Article> articles;
public CustomAdapter(Context c, ArrayList<Article> articles) {
this.c = c;
this.articles = articles;
}
@Override
public int getCount() {
return articles.size();
}
@Override
public Object getItem(int position) {
return articles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
TextView titleTxt= (TextView) convertView.findViewById(R.id.titleTxt);
TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);
final Article article= (Article) this.getItem(position);
titleTxt.setText(article.getTitle());
descTxt.setText(article.getDescription().substring(0, 120));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c, article.getTitle(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
Downloader.java
public class Downloader extends AsyncTask<Void,Void,Object> {
Context c;
String urlAddress;
ListView lv;
ProgressDialog pd;
public Downloader(Context c, String urlAddress, ListView lv) {
this.c = c;
this.urlAddress = urlAddress;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Fetch data");
pd.setMessage("Fetching Data...Please wait");
pd.show();
}
@Override
protected Object doInBackground(Void... params) {
return this.downloadData();
}
@Override
protected void onPostExecute(Object data) {
super.onPostExecute(data);
pd.dismiss();
if(data.toString().startsWith("Error"))
{
Toast.makeText(c,data.toString(),Toast.LENGTH_SHORT).show();
}else {
//PARSE
new RSSParser(c, (InputStream) data,lv).execute();
}
}
private Object downloadData()
{
Object connection=Connector.connect(urlAddress);
if(connection.toString().startsWith("Error"))
{
return connection.toString();
}
try
{
HttpURLConnection con= (HttpURLConnection) connection;
int responseCode=con.getResponseCode();
if(responseCode==con.HTTP_OK)
{
InputStream is=new BufferedInputStream(con.getInputStream());
return is;
}
return ErrorTracker.RESPONSE_ERROR+con.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
return ErrorTracker.IO_EROR;
}
}
}
ErrorTracker.java
public class ErrorTracker {
public final static String WRONG_URL_FORMAT="Error : Wrong URL Format";
public final static String CONNECTION_ERROR="Error : Unable To Establish Connection";
public final static String IO_EROR="Error : Unable To Read";
public final static String RESPONSE_ERROR="Error : Bad Response - ";
}
RSSParser.java
public class RSSParser extends AsyncTask<Void,Void,Boolean> {
Context c;
InputStream is;
ListView lv;
ProgressDialog pd;
ArrayList<Article> articles=new ArrayList<>();
public RSSParser(Context c, InputStream is, ListView lv) {
this.c = c;
this.is = is;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Parse data");
pd.setMessage("Parsing Data...Please wait");
pd.show();
}
@Override
protected Boolean doInBackground(Void... params) {
return this.parseRSS();
}
@Override
protected void onPostExecute(Boolean isParsed) {
super.onPostExecute(isParsed);
pd.dismiss();
if(isParsed)
{
//BIND
lv.setAdapter(new CustomAdapter(c,articles));
}else {
Toast.makeText(c,"Unable To Parse",Toast.LENGTH_SHORT).show();
}
}
private Boolean parseRSS()
{
try
{
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
XmlPullParser parser=factory.newPullParser();
parser.setInput(is,null);
int event=parser.getEventType();
String value=null;
articles.clear();
Article article=new Article();
do {
String name=parser.getName();
switch (event)
{
case XmlPullParser.START_TAG:
if(name.equals("item"))
{
article=new Article();
}
break;
case XmlPullParser.TEXT:
value=parser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title"))
{
article.setTitle(value);
}else if(name.equals("description"))
{
article.setDescription(value);
}
if(name.equals("item"))
{
articles.add(article);
}
break;
}
event=parser.next();
}while (event != XmlPullParser.END_DOCUMENT);
return true;
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
和MainActivity.java
ListView lv = (ListView)findViewById(R.id.listView);
new Downloader(MainActivity.this,urlAddress,lv).execute();
请问有什么解决办法吗?记住,是博主rss。
注意事项:您的 RSS 使用的是 Atom 格式,因此请确保您的代码基于该格式。 Atom specifications
有关实施 RSS 解析的分步指南,请查看来自 Androoid documentation 的精彩指南。
尝试将 ?alt=rss
添加到 blogspot 提要 url :
https://mystat115.blogspot.com/feeds/posts/default?alt=rss
我是 android 开发的新手(初学者),我在 Blogger.com 上有博客,想获取 RSS 数据并将其解析为 Android ListView,当我使用时,例如 https://www.sammobile.com/feeds/ it works, but when I change url adress to my blog rss adress: https://mystat115.blogspot.com/feeds/posts/default 它什么都没有..只是空白 activity...
这是我也用过的代码...
Article.Java
public class Article {
String title,description;
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;
}
}
Connector.Java
public class Connector {
public static Object connect(String urlAddress)
{
try
{
URL url=new URL(urlAddress);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
con.setDoInput(true);
return con;
} catch (MalformedURLException e) {
e.printStackTrace();
return ErrorTracker.WRONG_URL_FORMAT;
} catch (IOException e) {
e.printStackTrace();
return ErrorTracker.CONNECTION_ERROR;
}
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Article> articles;
public CustomAdapter(Context c, ArrayList<Article> articles) {
this.c = c;
this.articles = articles;
}
@Override
public int getCount() {
return articles.size();
}
@Override
public Object getItem(int position) {
return articles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
TextView titleTxt= (TextView) convertView.findViewById(R.id.titleTxt);
TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);
final Article article= (Article) this.getItem(position);
titleTxt.setText(article.getTitle());
descTxt.setText(article.getDescription().substring(0, 120));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c, article.getTitle(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
Downloader.java
public class Downloader extends AsyncTask<Void,Void,Object> {
Context c;
String urlAddress;
ListView lv;
ProgressDialog pd;
public Downloader(Context c, String urlAddress, ListView lv) {
this.c = c;
this.urlAddress = urlAddress;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Fetch data");
pd.setMessage("Fetching Data...Please wait");
pd.show();
}
@Override
protected Object doInBackground(Void... params) {
return this.downloadData();
}
@Override
protected void onPostExecute(Object data) {
super.onPostExecute(data);
pd.dismiss();
if(data.toString().startsWith("Error"))
{
Toast.makeText(c,data.toString(),Toast.LENGTH_SHORT).show();
}else {
//PARSE
new RSSParser(c, (InputStream) data,lv).execute();
}
}
private Object downloadData()
{
Object connection=Connector.connect(urlAddress);
if(connection.toString().startsWith("Error"))
{
return connection.toString();
}
try
{
HttpURLConnection con= (HttpURLConnection) connection;
int responseCode=con.getResponseCode();
if(responseCode==con.HTTP_OK)
{
InputStream is=new BufferedInputStream(con.getInputStream());
return is;
}
return ErrorTracker.RESPONSE_ERROR+con.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
return ErrorTracker.IO_EROR;
}
}
}
ErrorTracker.java
public class ErrorTracker {
public final static String WRONG_URL_FORMAT="Error : Wrong URL Format";
public final static String CONNECTION_ERROR="Error : Unable To Establish Connection";
public final static String IO_EROR="Error : Unable To Read";
public final static String RESPONSE_ERROR="Error : Bad Response - ";
}
RSSParser.java
public class RSSParser extends AsyncTask<Void,Void,Boolean> {
Context c;
InputStream is;
ListView lv;
ProgressDialog pd;
ArrayList<Article> articles=new ArrayList<>();
public RSSParser(Context c, InputStream is, ListView lv) {
this.c = c;
this.is = is;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Parse data");
pd.setMessage("Parsing Data...Please wait");
pd.show();
}
@Override
protected Boolean doInBackground(Void... params) {
return this.parseRSS();
}
@Override
protected void onPostExecute(Boolean isParsed) {
super.onPostExecute(isParsed);
pd.dismiss();
if(isParsed)
{
//BIND
lv.setAdapter(new CustomAdapter(c,articles));
}else {
Toast.makeText(c,"Unable To Parse",Toast.LENGTH_SHORT).show();
}
}
private Boolean parseRSS()
{
try
{
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
XmlPullParser parser=factory.newPullParser();
parser.setInput(is,null);
int event=parser.getEventType();
String value=null;
articles.clear();
Article article=new Article();
do {
String name=parser.getName();
switch (event)
{
case XmlPullParser.START_TAG:
if(name.equals("item"))
{
article=new Article();
}
break;
case XmlPullParser.TEXT:
value=parser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title"))
{
article.setTitle(value);
}else if(name.equals("description"))
{
article.setDescription(value);
}
if(name.equals("item"))
{
articles.add(article);
}
break;
}
event=parser.next();
}while (event != XmlPullParser.END_DOCUMENT);
return true;
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
和MainActivity.java
ListView lv = (ListView)findViewById(R.id.listView);
new Downloader(MainActivity.this,urlAddress,lv).execute();
请问有什么解决办法吗?记住,是博主rss。
注意事项:您的 RSS 使用的是 Atom 格式,因此请确保您的代码基于该格式。 Atom specifications
有关实施 RSS 解析的分步指南,请查看来自 Androoid documentation 的精彩指南。
尝试将 ?alt=rss
添加到 blogspot 提要 url :
https://mystat115.blogspot.com/feeds/posts/default?alt=rss