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 );
}
}) ;