1. activity_main.xml
4. ListCityAdapter.java
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SimpleClient" >
<ListView
android:id="@+id/listViewCity"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
2. 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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/listItemCity"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="15dp"
/>
</LinearLayout>
3. SimpleClient.java
package com.example.simpleclient;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class SimpleClient extends Activity {
private static final int FINAL_DONE = 1; //页面加载完成
private static final int CITY_DONE = 2; //城市加载完成
private ListView mListViewCity;
private ListCityAdapter mListCityAdapter;
private ArrayList<String> mListItems = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mThread.start();
}
private void initView() {
mListViewCity = (ListView) findViewById(R.id.listViewCity);
mListCityAdapter = new ListCityAdapter(this, mListItems);
mListViewCity.setAdapter(mListCityAdapter);
}
private Thread mThread = new Thread(new Runnable() {
@Override
public void run() {
HttpClient client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpPost get = new HttpPost("http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getSupportCity");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("byProvinceName", "ALL"));
try {
get.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(get);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
}
Log.i("tag", "百度返回的数据:" + builder.toString());
byte[] midbytes=builder.toString().getBytes("UTF8");
parseXML(midbytes);
Message message = new Message();
message.what = FINAL_DONE;
myHandler.sendMessage(message);
Message message1 = new Message();
message1.what = CITY_DONE;
myHandler.sendMessage(message1);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FINAL_DONE:
Log.i("tag", "返回到主线程。");
break;
case CITY_DONE:
mListCityAdapter.notifyDataSetChanged();
break;
}
super.handleMessage(msg);
}
};
private boolean parseXML(byte[] bs) {
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder domBuilder = domfac.newDocumentBuilder();
// InputStream is = new FileInputStream(new File("D:/test1.xml"));
InputStream is = new ByteArrayInputStream(bs);
Document doc = domBuilder.parse(is);
Element root = doc.getDocumentElement();
NodeList books = root.getChildNodes();
if(books!=null){
Log.i("tag", "length="+books.getLength());
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
if(book.getNodeType()==Node.ELEMENT_NODE) {
Log.i("tag", "type 00000 ");
String name = book.getTextContent();
Log.i("tag", "type 11111 name="+name);
if (name != null) {
mListItems.add(book.getTextContent());
}
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
4. ListCityAdapter.java
package com.example.simpleclient;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListCityAdapter extends BaseAdapter{
private Context mContext;
private List<String> mListItems;
private LayoutInflater mListContainer;
public final class ListItemView {
public TextView title;
}
public ListCityAdapter(Context context, List<String>listItems) {
this.mContext = context;
mListContainer = LayoutInflater.from(mContext);
this.mListItems = listItems;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int selectID = position;
//自定义视图
ListItemView mListItemView = null;
if (convertView == null) {
mListItemView = new ListItemView();
//获取listItem布局文件的视图
convertView = mListContainer.inflate(R.layout.list_item, null);
//获取控件对象
mListItemView.title = (TextView)convertView.findViewById(R.id.listItemCity);
//设置控件集到convertView
convertView.setTag(mListItemView);
} else {
mListItemView = (ListItemView) convertView.getTag();
}
//设置文本
mListItemView.title.setText((String)mListItems.get(position));
return convertView;
}
}
5.ParseBean.java
package com.example.simpleclient;
import java.util.HashMap;
public class ParseBean {
private String mId;
public String getmId() {
return mId;
}
public void setmId(String string) {
this.mId = string;
}
}