AsyncHttpClient 获取图片和网页数据演示

AsyncHttpClient 极其好用,采用回调的方式并且是异步的,UI线程已经封装好。 https://github.com/loopj/android-async-http

这个例子较为简单。

先看运行结果


AsyncHttpClient 获取图片和网页数据演示_第1张图片


点击按钮 从网络上获取图片和baidu首页显示出来.

项目是直接用的源码,为了研究源码使用的。


AsyncHttpClient 获取图片和网页数据演示_第2张图片


public class HTTPHelper {

	public HTTPHelper() {
	}
	
	public void get(String url,ResponseHandlerInterface callback){
		AsyncHttpClient  client = new AsyncHttpClient();
		client.get(url,callback);
	}

}


public class MyActivity extends Activity  {

	final static String TAG = MyActivity.class.getCanonicalName();
	private final static String URL = "http://www.baidu.com";
	private final static String IMAGE_URL = "http://preview.quanjing.com/glow_foto001/gcr211129740.jpg";//图片资源
	private WebView webView;
	private ImageView imageView;
	private HTTPHelper helper;

	public MyActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		webView = (WebView) findViewById(R.id.webview);
		imageView = (ImageView) findViewById(R.id.img);
		helper = new HTTPHelper();
		findViewById(R.id.btn_http_get).setOnClickListener(
				new OnClickListener() {
					public void onClick(View view) {
						helper.get(URL, textCallback);//获取文本
					}
				});

		findViewById(R.id.btn_http_get_img).setOnClickListener(
				new OnClickListener() {
					public void onClick(View view) {
						helper.get(IMAGE_URL, binaryCallback);//获取图片
					}
				});
	}

	// 获取图片流
	BinaryHttpResponseHandler binaryCallback = new BinaryHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] binaryData) {
			Log.d(TAG, "onSuccess:" + statusCode + " binaryData:"
					+ binaryData.length);
			MyActivity.this.updateImage(binaryData);
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] binaryData, Throwable error) {
			Log.e(TAG, "onFailure:" + error + " statusCode:" + statusCode);
			
		}
	};

	// 获取文本流
	TextHttpResponseHandler textCallback = new TextHttpResponseHandler() {

		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			Log.e(TAG, "onFailure:" + throwable + " statusCode:" + statusCode);
		}

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				String responseString) {
			Log.d(TAG, "onSuccess:" + statusCode + " responseString:"
					+ responseString.length());
			MyActivity.this.updateWebView(responseString);
		}

	};

	public void updateImage(byte[] data) {
		Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
		imageView.setImageBitmap(b);
	}

	public void updateWebView(String data) {
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setDefaultTextEncodingName("UTF -8");
		webView.loadData(data, "text/html; charset=UTF-8", null);
	}
	
}



BinaryHttpResponseHandler 是Binary的请求回调,用在获取图片比较合适

TextHttpResponseHandler 是字符串文本类的请求回调,它的子类还有json获取的回调接口





    



That's all right~










你可能感兴趣的:(手机移动开发,Android,技术研究)