启用异步类获取数据
package com.bwie.myasynctask;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
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.xml.sax.SAXException;
import com.bwie.http_img_xml_caipu.ItemActivity;
import com.bwie.myadapter.HomeAdapter;
import com.bwie.mydefaulthander.HomeDefaultHander;
import com.bwie.vo.DataVO;
import com.bwie.vo.StepsVO;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MyAsyncTask extends AsyncTask<String, Integer, List<DataVO>> {
private Context context;
private ListView lv;
private String str_ed;
private List<DataVO> datalist;
public MyAsyncTask(Context context, ListView lv, String str_ed) {
super();
this.context = context;
this.lv = lv;
this.str_ed = str_ed;
}
@Override
protected List<DataVO> doInBackground(String... arg0) {
// TODO Auto-generated method stub
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(arg0[0]);
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("key", "3a3d0fa15180459cbf022d5d77e50de5"));
list.add(new BasicNameValuePair("menu", str_ed));
list.add(new BasicNameValuePair("rn", "10"));
list.add(new BasicNameValuePair("pn", "3"));
list.add(new BasicNameValuePair("dtype", "xml"));
try {
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,HTTP.UTF_8);
post.setHeader("Content_Type", "application/x-www-form-urlencoded;charset=utf-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode()==200) {
InputStream is=response.getEntity().getContent();
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
HomeDefaultHander dh=new HomeDefaultHander();
parser.parse(is, dh);
datalist = dh.getdatalist();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datalist;
}
@Override
protected void onPostExecute(final List<DataVO> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
lv.setAdapter(new HomeAdapter(context, result));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent(context, ItemActivity.class);
intent.putExtra("list",(Serializable)result.get(arg2).steps);
context.startActivity(intent);
}
});
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
自定义类
package com.bwie.mydefaulthander;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
import com.bwie.vo.DataVO;
import com.bwie.vo.StepsVO;
public class HomeDefaultHander extends DefaultHandler {
private List<DataVO> data;
private int mun=0;
private String flag;
private DataVO dataVO;
private StepsVO stepsVO;
//开始解析文档
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
data = new ArrayList<DataVO>();
Log.e("TAG", "开始解析文档");
}
//开始解析标签
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
Log.e("TAG", "解析标签开始");
flag = localName;
if (localName.equals("item")&&mun==0) {
dataVO = new DataVO();
mun=1;
}else if (localName.equals("item")&&mun==2) {
stepsVO = new StepsVO();
mun=2;
}else if(localName.equals("steps")){
dataVO.steps=new ArrayList<StepsVO>();
mun=2;
}
}
//解析文本
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String text=new String(ch, start, length);
Log.e("TAG", "解析文本"+text);
if (flag.equals("title")) {
dataVO.setTitle(text);
} else if (flag.equals("tags")){
dataVO.setTags(text);
}else if (flag.equals("imtro")){
dataVO.setImtro(text);
}else if (flag.equals("ingredients")){
dataVO.setIngredients(text);
}else if (flag.equals("burden")){
dataVO.setBurden(text);
}else if (flag.equals("item")&&mun==1){
dataVO.setAlbums(text);
}else if (flag.equals("img")){
stepsVO.setImg(text);
}else if (flag.equals("step")){
stepsVO.setStep(text);
}
}
//解析标签结束
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (localName.equals("item")&&mun==2) {
dataVO.steps.add(stepsVO);
mun=2;
}else if(localName.equals("item")&&mun==0){
data.add(dataVO);
mun=0;
}else if(localName.equals("steps")){
mun=0;
}
flag="";
}
//解析文档结束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
//得到外层集合
public List<DataVO> getdatalist(){
return data;
}
}
来源:https://www.cnblogs.com/pingguo/p/5005612.html