Android-async-http架構十分好用,而且他的優點在于能很好管理項目開發中的接口。
先下載下傳這個架構
Github位址:
https://github.com/loopj/android-async-http
或是直接到我上傳的這個位址下面下載下傳這個jar包
http://download.csdn.net/detail/u011539882/8973599
準備好了以後将android-async-http 引入到工程中,這裡就不再闡述如何引入
利用這個開架構,有兩部,第一步建立一個URL類或是叫做接口管理類,用于存放所有請求的位址,第二部在你的activity裡面或其他地方引用這個管理類來實作網絡請求。
這次我們來做一個搜尋”琴吹柚”關鍵字的請求并顯示頁面文字
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.netrequestdemo.MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="5000dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Url管理類:
package com.example.netrequestdemo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* Url管理類這裡隻寫了一個,實際開發中有很多請求
*
*/
public class URLManage {
// 以百度搜尋為例
//https://www.baidu.com/s?wd=
// https://www.baidu.com/s?wd=琴吹柚
public final static String HOST = "https://www.baidu.com/";
private static AsyncHttpClient client = new AsyncHttpClient(); // 執行個體化對象
static {
client.setTimeout(11000); // 設定連結逾時,如果不設定,預設為10s
}
public static void showInfos(String string, JsonHttpResponseHandler res) {
String urlString = HOST + "s";
RequestParams params = new RequestParams(); // 綁定參數
params.put("wd", string);// 前面的key就是連接配接中所對應的字段
get(urlString, params, res);
}
/**
* 拼接位址并請求
*
* @param urlString
* @param params
* @param res
*/
private static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) {
System.out.println((urlString + "?" + params.toString()));//可以看下請求的位址
client.get(urlString, params, res);
}
}
MainActivity
package com.example.netrequestdemo;
import org.apache.http.Header;
import org.json.JSONObject;
import com.loopj.android.http.JsonHttpResponseHandler;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView main_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_tv = (TextView) findViewById(R.id.main_tv);
//網絡請求 new JsonHttpResponseHandler并重寫裡面的兩個方法
URLManage.showInfos("琴吹柚", new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, String responseBody) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, responseBody);
// System.out.println(responseBody.toString());//請求的網頁内容文字形式
main_tv.setText(responseBody.toString());//設定内容
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) {
super.onFailure(statusCode, headers, e, errorResponse);
Toast.makeText(MainActivity.this, "請求失敗", Toast.LENGTH_SHORT).show();
}
});
}
}
運作效果:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0NXYFhGd192UvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DO4QTNygDM2EzNwgDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
Demo下載下傳