在android studio中使用android-async-http框架時發現報錯,錯誤是:org.apache.http.Header類找不到了。后來發現是Http Client在android 6.0 被移除了。官方文檔的解決方案是:
If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:
需要在app目錄下的build.gradle中進行以下配置
android { useLibrary 'org.apache.http.legacy' }
重建項目錯誤就消失了
初步使用步驟:
導入android-async-http框架的.jar包
有兩種實現方式,get和post。兩種方式的主要區別是參數傳遞的方式不同
package com.contentprovide.liuliu.async_demo_2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import org.apache.http.Header; public class MainActivity extends AppCompatActivity { // 聲明一個請求客戶端對象 AsyncHttpClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 實例化客戶端對象 client = new AsyncHttpClient(); // async_Get(); async_Post(); } //Get請求方式的使用 private void async_Get() { String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&dtype=json&key=72a24be15b8b58c599d4d2f994e26cdb"; // 調用客戶端對象的get方法進行網絡請求並返回數據 client.get(url, new AsyncHttpResponseHandler() { // 請求成功 @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { Toast.makeText(getApplicationContext(), new String(bytes), Toast.LENGTH_LONG).show(); } // 請求失敗 @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }); } //Post請求方式的使用 private void async_Post() { String url = "http://apis.juhe.cn/mobile/get?"; RequestParams params = new RequestParams(); params.add("phone","13666666666"); params.add("key","72a24be15b8b58c599d4d2f994e26cdb"); client.post(url, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { Toast.makeText(getApplicationContext(),new String(bytes),Toast.LENGTH_LONG).show(); } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }); } }
android-async-http回調邏輯的二次封裝
在正式項目中我們需要將這兩種請求方式封裝起來可以隨時調用,還可以進行自定制
步驟一:自定義一個類繼承AsyncHttpResponseHandler類,重寫AsyncHttpResponseHandler類中的Get和Post方法,以便可以在工具類中調用
package com.contentprovide.liuliu.async_demo_2; import com.loopj.android.http.AsyncHttpResponseHandler; import org.apache.http.Header; /** * Created by liuliu on 2018/3/15. * * 創建一個類繼承AsyncHttpResponseHandler類 * */ public class NetCallBack extends AsyncHttpResponseHandler { //重寫父類中請求成功的方法 @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { } // 重寫父類中請求失敗的方法 @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }
步驟二:自定義一個工具類,在里面聲明網絡請求的客戶端對象以及創建自定義的Get和Post方法
package com.contentprovide.liuliu.async_demo_2; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.RequestParams; /** * Created by liuliu on 2018/3/15. * * 創建一個工具類,存放網絡請求的客戶端對象和自定義的Get,Post方法 * */ public class RequestUtils { // 創建客戶端對象 public static AsyncHttpClient client = new AsyncHttpClient(); // 自定義Get請求方法 public static void ClientGet(String url, NetCallBack netCallBack){ client.get(url,netCallBack); } // 自定義Post請求方法 public static void ClientPost(String url,RequestParams requestParams,NetCallBack netCallBack){ client.post(url, requestParams, netCallBack); } }
接下來就可以在主類中直接調用這兩個方法了
package com.contentprovide.liuliu.async_demo_2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.RequestParams; import org.apache.http.Header; public class MainActivity extends AppCompatActivity { // 聲明一個請求客戶端對象 AsyncHttpClient client; TextView mytext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mytext = (TextView) findViewById(R.id.mytext); // 實例化客戶端對象 client = new AsyncHttpClient(); // async_Get(); async_Post(); } //Get請求方式的使用 private void async_Get() { String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&dtype=json&key=72a24be15b8b58c599d4d2f994e26cdb"; RequestUtils.ClientGet(url,new NetCallBack(){ @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { super.onSuccess(i, headers, bytes); mytext.setText(new String(bytes)); } }); } //Post請求方式的使用 private void async_Post() { String url = "http://apis.juhe.cn/mobile/get?"; RequestParams params = new RequestParams(); params.add("phone", "13666666666"); params.add("key", "72a24be15b8b58c599d4d2f994e26cdb"); RequestUtils.ClientPost(url,params,new NetCallBack(){ @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { super.onSuccess(i, headers, bytes); mytext.setText(new String(bytes)); } }); } }