今天是来成都实训的第三天,一如既往的坐在电脑前,敲代码,解决BUG,做代码的搬运工,标准的CV战士,不过,每天都还是有些收获,一边写着自己的日记本,一边听老师讲些基础的知识,虽然分心了,不过收获还是算满意了。
流星追忆日记本总结:
1.完成了在写日记时,可以插入表情图片的功能;
2.完成了将表情符号替换为表情图片的功能;
3.优化了部分UI。
优化前:
优化后:
今天学会了用Pull方式解析XML,直接上案例,做一个简单的RSS阅读器。
知识点:Pull是Android内置的xml解析器。Pull解析器的运行方式与SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型节点的值。
步骤:
1) 取得pull解析器 XmlPullParser parser = Xml.newPullParser();
2) 设置解析器的输入流 parser.setInput(inputStream,“utf-8”);
3) 产生第一个事件 int event = parser.getEventType();
4) 开始解析,循环,处理标签;
5) 解析结束。
人人网RSS:http://www.people.com.cn/rss/politics.xml
一、新建Android工程,工程结构图如下:
二、各部分代码
MainActivity.java
package com.scitc.RSSreader;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.example.RSSreader.R;
import com.scitc.RSSreader.adapter.RSSAdapter;
import com.scitc.RSSreader.entity.RSSItem;
import com.scitc.RSSreader.utils.DataUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv_app;
private final int RESPONSE_CODE = 1;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RESPONSE_CODE:
List<RSSItem> apps=(List<RSSItem>) msg.obj;
RSSAdapter adapter=new RSSAdapter(getApplicationContext(),R.layout.list_item,apps);
lv_app.setAdapter(adapter);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(DataUtil.URL);
HttpResponse httpResponse = httpClient.execute(httpGet);
// 判断响应是否成功
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,@H_338_301@"utf-8");
List<RSSItem> apps= getRSSItems(response);
Message message=new Message();
message.what=RESPONSE_CODE;
message.obj=apps;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/** * 用pull方式解析XML * * @param response */
private List<RSSItem> getRSSItems(String is) {
List<RSSItem> list = null;
RSSItem item = null;
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader(is));
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<RSSItem>();
break;
case XmlPullParser.START_TAG:
if (@H_338_301@"item".equals(parser.getName())) {
item = new RSSItem();
}
if (item != null) {
if (@H_338_301@"title".equals(parser.getName())) {
item.setTitle(parser.nextText());
} else if (@H_338_301@"link".equals(parser.getName())) {
item.setLink(parser.nextText());
} else if (@H_338_301@"description".equals(parser.getName())) {
item.setDescription(parser.nextText());
} else if (@H_338_301@"pubDate".equals(parser.getName())) {
item.setPubDate(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG:
if (@H_338_301@"item".equals(parser.getName())) {
list.add(item);
item = null;
}
break;
}
type = parser.next();
}
return list;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private void initView() {
lv_app = (ListView) findViewById(R.id.RSS_list);
}
}
RSSAdapter.java
package com.scitc.RSSreader.adapter;
import java.util.List;
import com.example.RSSreader.R;
import com.scitc.RSSreader.entity.RSSItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class RSSAdapter extends ArrayAdapter<RSSItem> {
private int resourceId;
/** * * @param context 上下文对象 * @param textViewResourceId 布局文件 * @param objects 集合对象 */
public RSSAdapter(Context context,int textViewResourceId,List<RSSItem> objects) {
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
RSSItem RSSItem = getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId,null);
} else {
view = convertView;
}
TextView tvIdText = (TextView) view.findViewById(R.id.tv_title);
tvIdText.setText(RSSItem.getTitle()+@H_338_301@"");
TextView tvNameText = (TextView) view.findViewById(R.id.tv_pubDate);
tvNameText.setText(RSSItem.getPubDate());
return view;
}
}
RSSItem.java
package com.scitc.RSSreader.entity;
public class RSSItem {
private String title;
private String pubDate;
private String description;
private String link;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
DataUtil.java
package com.scitc.RSSreader.utils;
public class DataUtil {
public final static String URL=@H_338_301@"http://www.people.com.cn/RSS/politics.xml";
}
layout目录下的布局文件
activity_main.xml
<RelativeLayout xmlns:android=@H_338_301@"http://schemas.android.com/apk/res/android"
xmlns:tools=@H_338_301@"http://schemas.android.com/tools"
android:layout_width=@H_338_301@"match_parent"
android:layout_height=@H_338_301@"match_parent"
android:paddingBottom=@H_338_301@"@dimen/activity_vertical_margin"
android:paddingLeft=@H_338_301@"@dimen/activity_horizontal_margin"
android:paddingRight=@H_338_301@"@dimen/activity_horizontal_margin"
android:paddingTop=@H_338_301@"@dimen/activity_vertical_margin"
tools:context=@H_338_301@"com.scitc.RSSreader.MainActivity" >
<ListView
android:id=@H_338_301@"@+id/RSS_list"
android:layout_width=@H_338_301@"wrap_content"
android:layout_height=@H_338_301@"wrap_content"/>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" >
<TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee"/>
<TextView android:id="@+id/tv_pubDate" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
这就是所有的代码部分了,最后添加访问网络权限
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
运行结果图: