import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.HttpHandler.State;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.yang.bean.Books;
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.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private boolean isFlag = true;
private List<Books> list;
private String tagNmae = "";
private Books books;
private int eventType;
private ListView lv;
//
private List<String> name = null;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<Books> list=(List<Books>) msg.obj;
name = new ArrayList<String>();
for (Books books : list) {
name.add(books.getCatalog());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, name);
lv.setAdapter(adapter);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.main_lv);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getHttpUtils();
}
}).start();
}
private void getHttpUtils() {
// TODO Auto-generated method stub
HttpUtils httpUtils = new HttpUtils();
// 设置当前请求的缓存时间
httpUtils.configCurrentHttpCacheExpiry(0 * 1000);
// 设置线程数
httpUtils.configRequestThreadPoolSize(10);
httpUtils.configResponseTextCharset("utf-8");
httpUtils
.send(HttpMethod.GET,
"url",
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0,
String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
// Log.i("yang", arg0.result);
List<Books> book = getXmlBook(arg0.result);
Message msg=new Message();
msg.obj = book;
handler.sendMessage(msg);
}
});
}
protected List<Books> getXmlBook(String result) {
// TODO Auto-generated method stub
try {
XmlPullParser xml = Xml.newPullParser();
InputStream in = new ByteArrayInputStream(result.getBytes("UTF-8"));
xml.setInput(in, "UTF-8");
eventType = xml.getEventType();
while (isFlag) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<Books>();
break;
case XmlPullParser.START_TAG:
tagNmae = xml.getName();
if(tagNmae.equals("item")){
books = new Books();
}
break;
case XmlPullParser.TEXT:
if(tagNmae.equals("id")){
books.setId(xml.getText());
}else if(tagNmae.equals("catalog")){
books.setCatalog(xml.getText());
}
break;
case XmlPullParser.END_TAG:
tagNmae = xml.getName();
if(tagNmae.equals("item")){
list.add(books);
}
tagNmae = "";
break;
case XmlPullParser.END_DOCUMENT:
//Log.i("yang", list.toString());
isFlag = false;
return list;
}
eventType = xml.next();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
第三方xUtils中HttpUtils请求网络数据与Pull解析
最新推荐文章于 2021-11-12 11:27:11 发布