1. Volley —— Google出品的android平台轻量级网络库
优点:扩展性强,请求队列管理
基本介绍:http://blog.csdn.net/itachi85/article/details/51043704
github镜像: https://github.com/mcxiaoke/android-volley
2. okhttp —— Square出品的java网络库,android4.4以后已作为默认的HTTP连接实现
优点:支持SPDY,连接池,传输效率的各种优化
基本介绍:http://blog.csdn.net/itachi85/article/details/51142486
源码:https://github.com/square/okhttp
官方wiki:https://github.com/square/okhttp/wiki
3. Volley + OkHttp 结合
由于 Volley 和 OKHttp 各有优缺点,Volley 不支持 HTTPS,而 OKHttp 支持 HTTPS。Volley的扩展性又非常强,因此我们可以将 Volley 和 OKHTTP结合使用。
4. 添加依赖
// Volley
compile 'com.android.volley:volley:1.0.0'
// okhttp
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
// gson
compile 'com.google.code.gson:gson:2.7'
5. AppConfig.java —— 全局Application的配置
package com.qianxingzhe.mynetwork.app;
import android.app.Application;
import com.android.volley.RequestQueue;
import com.qianxingzhe.mynetwork.network.RequestManager;
/**
* Created by lunyi.yly on 16/9/5.
*/
public class AppConfig extends Application {
private static AppConfig instance;
@Override
public void onCreate() {
super.onCreate();
initAppConfig();
initNetWork();
}
private void initNetWork() {
if (RequestManager.getRequestQueueNoThrowable() == null) {
RequestManager.init(this);
}
}
private void initAppConfig() {
if (instance == null) {
instance = this;
}
}
public static Application getApp() {
return instance;
}
public static RequestQueue requestQueue() {
return RequestManager.getRequestQueue();
}
}
6. OkHttpStack
package com.qianxingzhe.mynetwork.network;
import com.android.volley.toolbox.HurlStack;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;
import java.util.Arrays;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
/**
* Created by lunyi.yly on 16/9/5.
*/
public class OkHttpStack extends HurlStack {
private final OkUrlFactory okUrlFactory;
public OkHttpStack() {
this(new OkUrlFactory(getUnsafeOkHttpClient()));
}
public OkHttpStack(OkUrlFactory okUrlFactory) {
if (okUrlFactory == null) {
throw new NullPointerException("Client must not be null.");
}
this.okUrlFactory = okUrlFactory;
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
return okUrlFactory.open(url);
}
/**
* 支持HTTPS。按下面的注释进行
*
* @return
*/
private static OkHttpClient getUnsafeOkHttpClient() {
try {
/**
* 读取公钥证书内容
* 首先要把你的https公钥证书通过浏览器或者其他方法导出,放进android资源目录assets下。
* 然后AppConfig这个类是继承了Application的,android启动的时候会先执行它,getApp是一个单例模式的实现
*/
// 配置HTTPS时需要打开下面的注释
// InputStream inputStream = AppConfig.getApp().getAssets().open("https.cer");
//
// CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//
// KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
// keyStore.load(null);
//
// int index = 0;
// String certificateAlias = Integer.toString(index++);
// keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(inputStream));
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
// 配置HTTPS时需要把下面的注释打开
// trustManagerFactory.init(keyStore);
// 配置HTTPS时需要把下面这句注释掉
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManager}, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient client = new OkHttpClient
.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.build();
return client;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
7. RequestManager
package com.qianxingzhe.mynetwork.network;
import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by lunyi.yly on 16/9/5.
*/
public class RequestManager {
private static RequestQueue mRequestQueue;
private RequestManager() {
}
public static void init(Context context) {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(context, new OkHttpStack());
}
}
/**
* @return instance() of the queue
* @throws IllegalStateException if init has not yet been called
*/
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("Not initialized");
}
}
/**
* @return instance() of the queue
* @throws IllegalStateException if init has not yet been called
*/
public static RequestQueue getRequestQueueNoThrowable() {
return mRequestQueue;
}
}
9. MyJsonObjectRequest
package com.qianxingzhe.mynetwork.network;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.qianxingzhe.mynetwork.BuildConfig;
import com.qianxingzhe.mynetwork.app.AppConfig;
import org.json.JSONObject;
import java.util.Map;
/**
* Created by lunyi.yly on 16/9/6.
*/
public class MyJsonObjectRequest {
/**
* 请求类型。如: GET,POST等
*/
private int method;
/**
* 请求的URL
*/
private String url;
/**
* 请求的参数
*/
private JSONObject requestParameter;
/**
* 请求成功的响应
*/
private RequestNetWork requestNetWork;
public MyJsonObjectRequest(int method, String url, Map<String, Object> parameter, RequestNetWork requestNetWork) {
this.method = method;
this.url = url;
this.requestNetWork = requestNetWork;
initRequestParameter(parameter);
createJsonObjectRequest();
}
private void initRequestParameter(Map<String, Object> parameter) {
if (parameter == null) {
return;
}
this.requestParameter = new JSONObject(parameter);
}
private void createJsonObjectRequest() {
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(
method,
url,
requestParameter,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (BuildConfig.DEBUG) {
Log.e("TAG", response.toString());
}
requestNetWork.onSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (BuildConfig.DEBUG) {
Log.e("TAG", error.getMessage(), error);
}
Toast.makeText(AppConfig.getApp(), "网络请求失败", Toast.LENGTH_SHORT).show();
}
}
);
AppConfig.requestQueue().add(mJsonObjectRequest);
}
public interface RequestNetWork {
void onSuccess(JSONObject response);
}
}
10. 使用示例
MyJsonObjectRequest mRequest = new MyJsonObjectRequest(
Request.Method.POST,
"http://api.1-blog.com/biz/bizserver/article/list.do",
null,
new MyJsonObjectRequest.RequestNetWork() {
@Override
public void onSuccess(JSONObject response) {
Gson mGson = new Gson();
Article article = mGson.fromJson(response.toString(), Article.class);
Log.e("TAG", article.getDetail().get(0).getTitle());
}
}
);
11. AndroidManifest设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qianxingzhe.mynetwork">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".app.AppConfig"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>