AsyncHttpClient的用法

下载好AsyncHttpClient,可以直接使用它的jar包,也可以直接copy源代码进工程。SmartImageView框架也不错:可以缓存图片

1. AsyncHttpClient下载文本:

</pre><p></p><p></p><pre code_snippet_id="503731" snippet_file_name="blog_20141031_2_2488843" name="code" class="java">package com.test;

import org.apache.http.Header;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class AsyncActivity extends Activity {

	private EditText et;
	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et = (EditText) findViewById(R.id.et);
		tv = (TextView) findViewById(R.id.tv);
	}

	public void go(View v) {
		new AsyncHttpClient().get(et.getText().toString().trim(), new TextHttpResponseHandler() {
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				tv.setText(responseString);
			}
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				Toast.makeText(getApplicationContext(), "服务器忙!!! " + statusCode, Toast.LENGTH_SHORT).show();
			}
		});
	}
}

2.  AsyncHttpClient下载图片:

AsyncHttpClient的用法_第1张图片



你可能感兴趣的:(AsyncHttpClient的用法)