1,下載下傳一個服務端 tomcat
下載下傳後開始運作,将需要浏覽的東西,放在webapps-root檔案下
這裡假設有一個xml小檔案,接下來就開始上代碼了,
在同一個包下給mainactivity創造兩個class檔案,一個用來解析xml檔案(解析方式多種,有興趣可以上網查閱資料),一個用于存放資料
1,存放資料:
package com.example.xinwen;
public class News {
private String city;
private String temp;
private String wind;
private String pm250;
private String image;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
public String getTemp() {
return temp;
public void setTemp(String temp) {
this.temp = temp;
public String getWind() {
return wind;
public void setWind(String wind) {
this.wind = wind;
public String getPm250() {
return pm250;
public void setPm250(String pm250) {
this.pm250 = pm250;
public String getImage() {
return image;
public void setImage(String image) {
this.image = image;
2,解析xml檔案
(1)假設xml檔案長這樣,将它放進tomcat裡面即可
<?xml version="1.0" encoding="utf-8"?>
<weather>
<channel>
<city>北京</city>
<temp>25°</temp>
<image>http://192.168.1.101:8080/img/a.jpg</image>
<wind>1</wind>
<pm250>300</pm250>
</channel>
<channel>
<city>鄭州</city>
<temp>20°</temp>
<image>http://192.168.1.101:8080/img/b.jpg</image>
<wind>2</wind>
<channel>
<city>長春</city>
<temp>10°</temp>
<image>http://192.168.1.101:8080/img/c.jpg</image>
<wind>3</wind>
<pm250>100</pm250>
<city>沈陽</city>
<image>http://192.168.1.101:8080/img/d.jpg</image>
<pm250>50</pm250>
</weather>
(2)開始解析
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
public class XmlPasser {
//解析xml的業務方法
public static List<News> parserXml(InputStream in) throws Exception{
List<News> newsLists=null;
News news=null;
//擷取xml解析器
XmlPullParser parser=Xml.newPullParser();
//設定解析器,要解析的内容
parser.setInput(in,"utf-8");
//擷取要解析的事件類型
int type=parser.getEventType();
//不得向下解析
while(type!=XmlPullParser.END_DOCUMENT){
switch(type){
case XmlPullParser.START_TAG://解析開始節點
//[6]具體判斷一下是哪個标簽
if("weather".equals(parser.getName())){
newsLists=new ArrayList<News>();
}else if("channel".equals(parser.getName())){
news=new News();
}else if("city".equals(parser.getName())){
news.setCity(parser.nextText());
}else if("temp".equals(parser.getName())){
news.setTemp(parser.nextText());
}else if("image".equals(parser.getName())){
news.setImage(parser.nextText());
}else if("wind".equals(parser.getName())){
news.setWind(parser.nextText());
}else if("pm250".equals(parser.getName())){
news.setPm250(parser.nextText());
break;
case XmlPullParser.END_TAG://解析結束标簽
if("channel".equals(parser.getName())){
//把Javabean添加到集合
newsLists.add(news);
//不停向下解析
type=parser.next();
return newsLists;
3,好了副class檔案制作好了,就開始在mainactivity中制作正文了
import android.os.Bundle;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<News> newsLists;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//[2]準備listview要顯示的資料,去伺服器取資料進行封裝
initListData();
//準備listview的資料
private void initListData(){
new Thread(){
public void run(){
try {
//[2]去伺服器取資料http://192.168.1.104:8080/weather.xml
//[2.2]建立URL 對象指定我們要通路的 網址(路徑)
URL url = new URL(path);
//[2.3]拿到httpurlconnection對象 用于發送或者接收資料
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//[2.4]設定發送get請求
conn.setRequestMethod("GET");//get要求大寫 預設就是get請求
//[2.5]設定請求逾時時間
conn.setConnectTimeout(5000);
//[2.6]擷取伺服器傳回的狀态碼
int code = conn.getResponseCode();
//[2.7]如果code == 200 說明請求成功
if(code==200){
//[2.8]擷取伺服器傳回的資料 是以流的形式傳回的 由于把流轉換成字元串是一個非常常見的操作 是以我抽出一個工具類(utils)
InputStream in = conn.getInputStream();
newsLists = XmlPasser.parserXml(in);
runOnUiThread(new Runnable(){
public void run() {
// TODO 自動生成的方法存根
//更新ui把資料展示到子線程
lv.setAdapter(new MyAdapter());
});
} catch (Exception e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
};}.start();
private class MyAdapter extends BaseAdapter{
public int getCount() {
return newsLists.size();
public Object getItem(int arg0) {
return null;
public long getItemId(int arg0) {
return 0;
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView==null){
view=View.inflate(getApplicationContext(), R.layout.item, null);
}else{
view =convertView;
//找到控件顯示集合裡面的資料
ImageView iv_icon=(ImageView) view.findViewById(R.id.iv_icon);
TextView tv_title=(TextView) view.findViewById(R.id.tv_title);
TextView tv_desc=(TextView) view.findViewById(R.id.tv_desc);
TextView tv_type=(TextView) view.findViewById(R.id.tv_type);
//展示資料
tv_title.setText(newsLists.get(position).getCity());
tv_desc.setText(newsLists.get(position).getTemp());
String typee=newsLists.get(position).getWind();
String comment=newsLists.get(position).getPm250();
int type=Integer.parseInt(typee);
case 1:
tv_type.setText(comment+"國内");
case 2:
tv_type.setText("跟帖");
case 3:
tv_type.setText("國外");
return view;
至此src下的正文部分結束
我們接下來要來控制按鈕這些内容了,在res-layout下建立一個子xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/iv_icon"
android:ellipsize="end"
android:singleLine="true"
android:text="sadasaddsasdasdada"
android:textColor="#000000"
android:textSize="18sp" />
android:id="@+id/tv_desc"
android:layout_alignBottom="@+id/iv_icon"
android:layout_below="@id/tv_title"
android:layout_marginTop="6dp"
android:layout_toRightOf="@id/iv_icon"
android:maxLines="2"
android:text="啊結果了敬愛個路口就愛看的兩個件上的故事格式的公共的十大歌手大事"
android:textColor="#999999"
android:textSize="14sp" />
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_alignBottom="@id/iv_icon"
android:layout_alignParentRight="true"
android:layout_marginRight="3dp"
android:text="跟帖"
android:textColor="#ff0000"
</RelativeLayout>
這樣就制作成功了,最後勿忘給安卓adt一個上網許可(百度)
本文轉自眉間雪 51CTO部落格,原文連結:http://blog.51cto.com/13348847/1979569,如需轉載請自行聯系原作者