好久没逛简书了。这周公司的项目也已经愉快的迭代了新版本,对于之前一直存留的东西一直没怎么梳理,今天想说说这两年特别火的网络框架。okhttp我想大部分Android开发者都不陌生,因为它的到来。是我们Android开发者的一个福音,像之前我们一直都在用volley网络请求 、android-async-http、xUtils框架等等,他们都有自己的优势,详细使用我在这里就不一一介绍了,Github有很多这样的Demo、毕竟咱今天主要说的是okhttp,相比于上面说的那些,okhttp的使用就更多具体,好用,在业界的口碑也是非常好。再加上Google已经在6.0版本里面删除了HttpClient相关API,如果有些开发者还是想用的话只能自己手动添加jar包了。我觉得为了更好的在应对目前流行的网络访问,了解使用okhttp还是非常有必要的,开始进入正题:
- 在使用的时候,对于咱们Android Studio的用户,先添加依赖库:
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'com.squareup.okio:okio:1.7.0'
- 或者使用使用以下这个,这个结合Retrofit(retrofit就是对okhttp做了一层封装。把网络请求都交给给了Okhttp,我们只需要通过简单的配置就能使用retrofit来进行网络请求)使用会更灵活,下次有时间把这个可以做一遍文章写出来。
compile 'com.squareup.okhttp3:okhttp:3.2.0'
Get请求
在OKHttp中,每一次网络请求就是一个Request,我们只要在Request里填写我们需要的url,header等其他参数,再通过Request构造出Call,Call内部去请求参数,得到回复,并将返回调用者。即可
1.同步,异步请求
先声明 OkHttpClient client = new OkHttpClient();
然后在子线程去执行以下操作(因为android本身是不能在主线程做网络请求操作的,这样会阻塞UI线程因此我们需要自己开启一个线程)
Request request = new Request.Builder()
.url("https://www.baidu.com/")
.build();
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
Log.i("MainActivity",response.code());
Log.i("MainActivity",response.body().string());
}
},
- 异步请求的话改一丢丢就行了
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
//NOT UI Thread
if(response.isSuccessful()){
Log.i("MainActivity",response.code());
Log.i("MainActivity",response.body().string());
}
}
});
大致步骤就是这样了
2.POST请求
在进行post请求的时候,我们一般都是需要添加参数和header,甚至有请求的id和key
· 传入header
Request request = new Request.Builder()
.url("https://api.github.com/repos/square/okhttp/issues")
.header("User-Agent", "OkHttp Headers.java")
.addHeader("Accept", "application/json; q=0.5")
.addHeader("Accept", "application/vnd.github.v3+json")
.build();
· 使用RequestBody添加参数,类似表单提交
RequestBody formBody = new FormEncodingBuilder()
.add("name", "zhangsan") .add("password", "123456")
.add("subject", "subjectmit")
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
· 在传入header或者post参数都是传到Request里,最后的调用方式也和上面的GET方式一样
Response response = client.newCall(request)
.
if (response.isSuccessful()) {
return response.body().string();
}else {
throw new IOException("Unexpected code " + response);
}
· 上面是异步请求的方式,如果是异步的话execute()改成enqueue就行了
3.还有文件上传下载,提交表达等操作,都在以下代码中进行了简单的封装
package com.lukey.okhttp_demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Map;
/**
* creator Lukey on 2016/8/9
*/
public class OKManager {
private OkHttpClient mClient;
private volatile static OKManager manager;
private final String TAG = OKManager.class.getSimpleName();
private Handler mHandler;
//提交Json数据
private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
//提交字符串
private static final MediaType MEDIA_TYPE_MARKWODN = MediaType.parse("text/x-markdown;charset=utf-8");
public OKManager() {
mClient = new OkHttpClient();
mHandler = new Handler(Looper.getMainLooper());
}
//采用单例模式获取对象
public static OKManager getInstance() {
OKManager instance = null;
if (manager == null) {
synchronized (OKManager.class) {
if (instance == null) {
instance = new OKManager();
manager = instance;
}
}
}
return instance;
}
/**
* 同步请求,在android开发中不常用,因为会阻塞UI线程
* @param url
* @return
*/
private String syncGetByURL(String url) {
//构建一个request请求
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = mClient.newCall(request).execute();
//同步请求数据
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 请求指定的URL返回的结果是json字符串
*
* @param url
* @param callBack
*/
public void asyncJsonStringByURL(String url, final Func1 callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonStringMethod(response.body().string(), callBack);
}
}
});
}
/**
* 请求返回的json对象
*
* @param url
* @param callBack
*/
public void asyncJsonObjectByURL(String url, final Func4 callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(request.body().toString(), callBack);
}
}
});
}
/**
* 请求返回的字节对象
*
* @param url
* @param callBack
*/
public void asyncGetByteURL(String url, final Func2 callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessByteMethod(response.body().bytes(), callBack);
}
}
});
}
/**
* 请求返回的结果是一个imageview类型 bitmap类型
*
* @param url
* @param callBack
*/
public void asyncDownLoadIamgeByURL(String url, final Func3 callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response != null && response.isSuccessful()) {
byte[] data = response.body().bytes();
Bitmap bitmap = new
CropSquareTrans().transform(BitmapFactory.decodeByteArray(data, 0, data.length));
callBack.onResponse(bitmap);
}
}
});
}
/**
* 模拟表单提交
*
* @param url
* @param params
* @param callBack
*/
public void sendComplexForm(String url, Map<String, String> params, final Func4 callBack) {
FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
//表单对象
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
formEncodingBuilder.add(entry.getKey(), entry.getValue());
}
}
RequestBody requestBody = formEncodingBuilder.build();
//采用post方式提交
Request request = new Request.Builder().url(url).post(requestBody).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(response.body().string(), callBack);
}
}
});
}
/**
* 向服务器提交String请求
*
* @param url
* @param content
* @param callBack
*/
public void sendStringByPostMethod(String url, String content, final Func4 callBack) {
final Request request = new Request.Builder()
.url(url).post(RequestBody.create(MEDIA_TYPE_MARKWODN, content)).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (request != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(response.body().string(), callBack);
}
}
});
}
/**
* 请求返回的结果是json字符串
*
* @param jsonValue
* @param callBack
*/
private void onSuccessJsonStringMethod(final String jsonValue, final Func1 callBack) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.onResponse(jsonValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
/**
* 返回响应的结果是json对象
*
* @param jsonValue
* @param callBack
*/
private void onSuccessJsonObjectMethod(final String jsonValue, final Func4 callBack) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.onResponse(new JSONObject(jsonValue));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
/**
* 返回响应的是byte[] 数组
*
* @param data
* @param callBack
*/
private void onSuccessByteMethod(final byte[] data, final Func2 callBack) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.onResponse(data);
}
}
});
}
// 定义四个接口,对外部调用
interface Func1 {
void onResponse(String result);
}
interface Func2 {
void onResponse(byte[] result);
}
interface Func3 {
void onResponse(Bitmap bitmap);
}
interface Func4 {
void onResponse(JSONObject jsonObject);
}
}
4.最后在补充一个缓存数据的代码
· 先设置一个路径
private static String savePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/mydata/dataCash";
private static File mCashFile = new File(savePath);
private static int mCacheSize = 20 * 1024 * 1024;
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!ConnectionUtil.isConnection(LdApplication.getInstance())) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
} else {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_NETWORK)
.build();
}
Response originalResponse = chain.proceed(request);
if (ConnectionUtil.isConnection(LdApplication.getInstance())) {
//有网的时候读接口上的@Headers里的配置,可以在这里进行统一的设置
String cacheControl = request.cacheControl().toString();
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control",
"public, only-if-cached, max-stale=2419200")
.removeHeader("Pragma")
.build();
}
}
};
· 以下设置持久化Cookie
public static void setCookie() {
mClient.setCookieHandler(new CookieManager(
new PersistentCookieStore(MyApplication.getInstance()),
CookiePolicy.ACCEPT_ALL));
}
MyApplication.getInstance() ---->在Application用单例的方式实现
5.在主界面中调用
public class MainActivity extends AppCompatActivity {
private Button mButton;
private final static String TAG = MainActivity.class.getSimpleName();
private OKManager mOKManager;
private String image_path = "http://pic.mmfile.net/thumbs/2016/06/66970_13a13_236.jpg";
private String json_path = "http://api.avatardata.cn/Weather/Query?key=d79f47b8dcb74eb78ea3d74b88876145&cityname=%E6%B7%B1%E5%9C%B3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) this.findViewById(R.id.button);
mOKManager = OKManager.getInstance();
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOKManager.asyncJsonStringByURL(json_path, new OKManager.Func1() {
@Override
public void onResponse(String result) {
Logger.json(result.toString());
Log.d(TAG,result);
}
});
}});
}
}
· 布局很简单一个点击按钮和一个显示文本
· 然后别忘了在配置文件中添加权限哦~~~~
<uses-permission android:name="android.permission.INTERNET"/>