最简单的使用:
依赖:compile 'com.loopj.android:android-async-http:1.4.9'
1 创建一个AsyncHttpClient对象
2 调用get()方法即可
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess( response) {
System.out.println(response);
}
});
public class AsyncHttp extends Activity {
private String url="http://news-at.zhihu.com/api/4/themes";
TextView viewById;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewById = (TextView) findViewById(R.id.textView);
}
public void fromJson(View view) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
viewById.setText(responseString);
}
});
}
}
android-async-http库使用
最新推荐文章于 2024-08-13 08:51:48 发布