您的位置:首页 > 移动开发 > Android开发

ANdroid网易客户端

2015-07-03 09:41 711 查看
本博文主要记录一下做一个网易客户端的过程和感受。

这个APP采用的ListView这个控件,首先我们先在布局文件中定义一个ListView。

<ListView
android:id="@+id/lv_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

接下来,就是进行mainactivity代码片段的
package com.ytu.neteasy;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.image.SmartImageView;
import com.ytu.neteasy.entity.NewsInfo;

public class MainActivity extends Activity {
private final int SUCCESS=0;
private final int ERROR=1;
ListView lv_News;
List<NewsInfo> newsinfo;

private static final String TAG = "MainActivity";
private Handler handler=new Handler(){
/**
* 接受消息
*/
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS://访问成功
//给ListView列表绑定数据
MyAdapter adapter=new MyAdapter();//创建一个适配器
newsinfo=(List<NewsInfo>) msg.obj;
lv_News.setAdapter(adapter);
break;
case ERROR:
Toast.makeText(MainActivity.this, "当前网络崩溃了", 0).show();
break;
default:
break;
}
};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

private void init() {
lv_News=(ListView) findViewById(R.id.lv_news);

//抓取新闻数据
//getNewsFromNet();
new Thread(new Runnable() {

@Override
public void run() {
//返回时新闻集合
List<NewsInfo> newsList=getNewsFromNet();
//绑定数据
Message msg=new Message();
if(newsList!=null){
msg.what=SUCCESS;
msg.obj=newsList;
}else{
msg.what=ERROR;
}
handler.sendMessage(msg);
}
}).start();

}

/**
* 抓取网路数据的方法
* @return
*/
private List<NewsInfo> getNewsFromNet() {
HttpClient client=null;
try {
//定义一个客户端
client=new DefaultHttpClient();

//定义get方法
String uri="http://10.10.49.166:8080/Web025Server/new.xml";
HttpGet get=new HttpGet(uri);

//执行请求
HttpResponse response=client.execute(get);
int statuscode=response.getStatusLine().getStatusCode();
if(statuscode==200){
InputStream in=response.getEntity().getContent();
List<NewsInfo> newsListInfo = getNewListFromInputStream(in);
return newsListInfo;
}else{
Log.i(TAG, "请求失败"+statuscode);
}

} catch (Exception e) {
e.printStackTrace();
}finally{
if(client!=null){
client.getConnectionManager().shutdown();
}
}
return null;

}

//解析xml文件
/**
* 从流中解析新闻的集合
* @param in
* @return
* @throws Exception
*/
private List<NewsInfo> getNewListFromInputStream(InputStream in) throws Exception{

XmlPullParser parser = Xml.newPullParser();//创建一个pull解析器
parser.setInput(in, "utf-8");//指定解析流和编码
List<NewsInfo> newsListInfo=null;
NewsInfo newinfo=null;
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){//如果没有到达结尾处,继续循环
String tagName=parser.getName();//获得节点名称
switch (eventType) {
case XmlPullParser.START_TAG://开始节点<news>
if("news".equals(tagName)){
newsListInfo=new ArrayList<NewsInfo>();
}else if("new".equals(tagName)){
newinfo=new NewsInfo();
}else if("title".equals(tagName)){
newinfo.setTitle(parser.nextText());
}else if("detail".equals(tagName)){
newinfo.setDetail(parser.nextText());
}else if("comment".equals(tagName)){
newinfo.setComment(Integer.valueOf(parser.next()));
}else if("image".equals(tagName)){
newinfo.setImageUrl(parser.nextText());
}
break;
case XmlPullParser.END_TAG://结束节点</news>
if("new".equals(tagName)){
newsListInfo.add(newinfo);
}
break;
default:
break;
}

eventType=parser.next();//取下一个事件类型

}

return newsListInfo;

}

class MyAdapter extends BaseAdapter{

/**
* 返回列表的总长度
*/
@Override
public int getCount() {
return newsinfo.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

/**
* 返回一个列表的子条目的
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=null;
if(convertView==null){
LayoutInflater inflater=getLayoutInflater();
view=inflater.inflate(R.layout.listview_item, null);
}else{
view=convertView;
}

SmartImageView smicon=(SmartImageView) view.findViewById(R.id.sm_listview_icon);
TextView titleview=(TextView) view.findViewById(R.id.sm_listview_title);
TextView detailview=(TextView) view.findViewById(R.id.sm_listview_detail);
TextView commentview=(TextView) view.findViewById(R.id.sm_listview_comment);
NewsInfo newInfo=newsinfo.get(position);

smicon.setImageUrl(newInfo.getImageUrl());
titleview.setText(newInfo.getTitle());
detailview.setText(newInfo.getDetail());
commentview.setText(newInfo.getComment()+"跟帖");
return view;
}

}

}
接下来给大家展示listview的布局文件
<?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="wrap_content"
android:padding="5dp"
>
<com.loopj.android.image.SmartImageView
android:id="@+id/sm_listview_icon"
android:layout_width="100dp"
android:layout_height="60dp"

/>
<TextView
android:id="@+id/sm_listview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="这是标题"
android:layout_marginLeft="3dp"
android:layout_toRightOf="@id/sm_listview_icon"
android:textSize="17sp"
android:textColor="@android:color/black"
/>
<TextView
android:id="@+id/sm_listview_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是内容"
android:layout_toRightOf="@id/sm_listview_icon"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
android:layout_marginTop="3dp"
android:layout_below="@id/sm_listview_title"
android:layout_alignLeft="@id/sm_listview_title"
/>
<TextView
android:id="@+id/sm_listview_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="12sp"
android:textColor="#ff0000"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
这个项目主要用到了子线程,网络传输数据,处理输入流,以及XML文件的解析

服务器端的部分xml解析
<?xml version="1.0" encoding="UTF-8" ?>
<news>

<new>
<title>今日之声:北大雕塑被戴口罩</title>
<detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
<comment>681</comment>
<image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image>
</new>
<new>
<title>奥巴马见达赖是装蒜</title>
<detail>外文局- 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>
<comment>1359</comment>
<image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image>
</new>
<new>
<title>轻松一刻: 我要沉迷学习不自拔</title>
<detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>
<comment>10116</comment>
<image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image>
</new>
<new>
<title>男女那些事儿</title>
<detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail>
<comment>10339</comment>
<image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image>
</new>
<new>
<title>3Q大战宣判: 腾讯获赔500万</title>
<detail>最高法驳回360上诉, 维持一审宣判.</detail>
<comment>6427</comment>
<image>http://10.10.49.166:8080/Web025Server/images/1.jpg</image>
</new>
<new>
<title>今日之声:北大雕塑被戴口罩</title>
<detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
<comment>681</comment>
<image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image>
</new>
<new>
<title>奥巴马见达赖是装蒜</title>
<detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>
<comment>1359</comment>
<image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image>
</new>
<new>
<title>轻松一刻: 我要沉迷学习不自拔</title>
<detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>
<comment>10116</comment>
<image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image>
</new>
<new>
<title>男女那些事儿</title>
<detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail>
<comment>10339</comment>
<image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image>
</new>
<new>
<title>3Q大战宣判: 腾讯获赔500万</title>
<detail>最高法驳回360上诉, 维持一审宣判.</detail>
<comment>6427</comment>
<image>http://10.10.49.166:8080/Web025Server/images/1.jpg</image>
</new>
<new>
<title>今日之声:北大雕塑被戴口罩</title>
<detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
<comment>681</comment>
<image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image>
</new>
<new>
<title>奥巴马见达赖是装蒜</title>
<detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>
<comment>1359</comment>
<image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image>
</new>
<new>
<title>轻松一刻: 我要沉迷学习不自拔</title>
<detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>
<comment>10116</comment>
<image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image>
</new>
<new>
<title>男女那些事儿</title>
<detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail>
<comment>10339</comment>
<image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image>
</new>
</news>


本文出自 “Java大白的战地” 博客,请务必保留此出处http://8023java.blog.51cto.com/10117207/1670426
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: