android异步请求服务器数据示例
1、在android4.0以后的版本,主线程(ui线程)不在支持网络请求,原因大概是影响主线程,速度太慢,容易卡机,所以需要开启新的线程请求数据;
thread1 = new thread(){
@override
public void run() {
try {
url url = new url(weburlmanager.carsever_getcarsservlet);
httpurlconnection conn = (httpurlconnection) url.openconnection();
bufferedinputstream bis = new bufferedinputstream(conn.getinputstream());
//缓冲读取
byte[] data = new byte[1024];
int len = 0;
string bufferstring = "";
while((len = bis.read(data)) != -1){
bufferstring+=new string(data, 0, len);
}
carlist = new jsonarray(bufferstring.trim());
//system.out.println(carlist);
/*
for(int i=0;i
2、新线程完成后一启动,发现报错,空指针 nullpointerexception,要等待线程完毕后才能得到数据,下面是两种解决方法:
1)要么判断线程是否还活着;
2)要么在线程中设置一flag,结束后,更改其状态
/*
//等待线程thread1执行完毕
while(true){
if(thread1.isalive()){
try {
thread.sleep(500);
} catch (interruptedexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}else{
break;
}
}
*/
//当线程还没结束,就睡500毫秒ms
while(!flag){
try {
thread.sleep(500);
} catch (interruptedexception e) {
// todo auto-generated catch block
e.printstacktrace();
}}}
3、处理返回的json数据
1)向服务器请求json数据,保存在carlist
url url = new url(weburlmanager.carsever_getcarsservlet);
httpurlconnection conn = (httpurlconnection) url.openconnection();
bufferedinputstream bis = new bufferedinputstream(conn.getinputstream());
//缓冲读取
byte[] data = new byte[1024];
int len = 0;
string bufferstring = "";
while((len = bis.read(data)) != -1){
bufferstring+=new string(data, 0, len);
}
carlist = new jsonarray(bufferstring.trim());
2)解析json数据
jsonobject car = (jsonobject) getitem(position);
try {
//this.pic.setimagebitmap(carimagearray.get(position));
this.title.settext(car.getstring("title"));
this.describe.settext(car.getstring("describe"));
this.updatetime.settext(car.getstring("updatetime"));
this.price.settext(string.format("%.1f", car.getdouble("price"))+"万");
this.pic.settag(weburlmanager.carserver_car_image+car.getstring("image"));
new asyncviewtask().execute(this.pic);
} catch (jsonexception e1) {
e1.printstacktrace();
}
4、图片加载通常很慢,最好异步请求
异步请求类源代码
import java.io.inputstream;
import java.lang.ref.softreference;
import java.net.httpurlconnection;
import java.net.url;
import java.util.hashmap;
import android.graphics.drawable.drawable;
import android.os.asynctask;
import android.util.log;
import android.view.view;
import android.webkit.urlutil;
import android.widget.imageview;
public class asyncviewtask extends asynctask {
private view mview;
private hashmap> imagecache;
public asyncviewtask() {
imagecache = new hashmap>();
}
protected drawable doinbackground(view... views) {
drawable drawable = null;
view view = views[0];
if (view.gettag() != null) {
if (imagecache.containskey(view.gettag())) {
softreference cache = imagecache.get(view.gettag().tostring());
drawable = cache.get();
if (drawable != null) {
return drawable;
}
}
try {
if (urlutil.ishttpurl(view.gettag().tostring())) {// 如果为网络地址。则连接url下载图片
url url = new url(view.gettag().tostring());
httpurlconnection conn = (httpurlconnection) url.openconnection();
conn.setdoinput(true);
conn.connect();
inputstream stream = conn.getinputstream();
drawable = drawable.createfromstream(stream, "src");
stream.close();
} else {// 如果为本地数据,直接解析
drawable = drawable.createfrompath(view.gettag().tostring());
}
} catch (exception e) {
log.v("img", e.getmessage());
return null;
}
}
this.mview = view;
return drawable;
}
protected void onpostexecute(drawable drawable) {
if (drawable != null) {
imageview view = (imageview) this.mview;
view.setimagedrawable(drawable);
this.mview = null;
}}}
上一篇: android自定义toast(widget开发)示例
下一篇: android打开rar压缩文件