request = GalHttpRequest .requestWithURL (
this, PATH_BITMAP ,
Bitmap bitmap = request. startSyncRequestBitmap ();
title .setText ("异步请求InputStream" );
request = GalHttpRequest .requestWithURL (
this, PATH_INPUTSTREAM );
// 必须先设置回调函数,不然调用异步请求无效
request. setListener (
new GalHttpRequestListener () {
@Override
public
void loadFinished (
final InputStream is,
boolean fromcache ) {
//注意,因为返回的是InputStream,通常状况都须要长时间操做,因此,回调函数是在子线程调用
//所以使用handler
handler .post (
new Runnable() {
@Override
public
void run () {
textView .setText (is .toString ());
textView .setVisibility (View .VISIBLE );
}
}) ;
}
@Override
// 请求失败时,有可能能够从缓存里面读取数据返回
public
void loadFailed (
final HttpResponse respone ,
InputStream cacheInputStream ) {
handler .post (
new Runnable() {
@Override
public
void run () {
textView .setText (respone .toString ());
textView .setVisibility (View .VISIBLE );
}
}) ;
}
}) ;
request. startAsynchronous ();
request = GalHttpRequest .requestWithURL (
this, PATH_STRING );
//第一次调用startAsynRequestString或者startAsynRequestBitmap必须在主线程调用
//由于只有在主线程中调用才能够初始化GalHttprequest内部的全局句柄Handler
request. startAsynRequestString (
new GalHttpLoadTextCallBack () {
@Override
public
void textLoaded (String text ) {
//该部分容许于UI线程
textView .setText (text );
textView .setVisibility (View .VISIBLE );
}
}) ;
request = GalHttpRequest .requestWithURL (
this, PATH_BITMAP );
request. startAsynRequestBitmap (
new GalHttpLoadImageCallBack () {
@Override
public
void imageLoaded (Bitmap bitmap ) {
imageView .setImageBitmap (bitmap );
imageView .setVisibility (View .VISIBLE );
}
}) ;
title .setText ("组装http参数" );
//交给GalHttprequest自动组装
url中的参数
NameValuePair feedPair =
new BasicNameValuePair ("feed" ,"comments-rss2" );
request = GalHttpRequest .requestWithURL (
this, PATH_WITHPARAMS ,feedPair );
request. startAsynRequestString (
new GalHttpLoadTextCallBack () {
@Override
public
void textLoaded (String text ) {
//该部分容许于UI线程
textView .setText (text );
textView .setVisibility (View .VISIBLE );
}
}) ;