当前位置: 首页 > 工具软件 > OkHttps > 使用案例 >

android获取网络时间工具类,okHttps3请求网络工具类

左丘宜年
2023-12-01

注意点:

okhttp2.x和okhttp3中的回调都不是在UI线程中。

okhttp3同步请求: mOkHttpClient.newCall(request).execute();

okhttp3异步请求:mOkHttpClient.newCall(request).enqueue();

本工具类是对okhttp3基本请求的封装,而OkHttpFinal对okhttp进行了较为完善的封装,关于OkHttpFinal,可以参考链接: https://github.com/pengjianbo/OkHttpFinal

import java.io.IOException;

import java.util.Map;

import java.util.concurrent.TimeUnit;

import okhttp3.Callback;

import okhttp3.FormBody;

import okhttp3.MediaType;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.RequestBody;

import okhttp3.Response;

import android.os.Handler;

import android.os.Looper;

import android.util.Log;

/**

* okhttp请求

*

*/

public class HttpUtils {

private OkHttpClient client;

// 超时时间

public static final int TIMEOUT = 1000 * 60;

//json请求

public static final MediaType JSON = MediaType

.parse("application/json; charset=utf-8");

private Handler handler = new Handler(Looper.getMainLooper());

public HttpUtils() {

this.init();

}

private void init() {

client = new OkHttpClient();

// 设置超时时间

client.newBuilder().connectTimeout(TIMEOUT, TimeUnit.SECONDS)

.writeTimeout(TIMEOUT, TimeUnit.SECONDS)

.readTimeout(TIMEOUT, TimeUnit.SECONDS).build();

}

/**

* post请求,json数据为body

*

* [@param] url

* [@param]Json

* [@param]callback

*/

public void postJson(String url, String json, final HttpCallback callback) {

RequestBody body = RequestBody.create(JSON, json);

Request request = new Request.Builder().url(url).post(body).build();

client.newCall(request).enqueue(new Callback() {

[@Override]

public void onResponse(Response response) throws IOException {

// TODO Auto-generated method stub

if (response.isSuccessful()) {

onSuccess(callback, response.body().string());

} else {

onError(callback, response.message());

}

}

[@Override]

public void onFailure(Request arg0, IOException arg1) {

// TODO Auto-generated method stub

onError(callback, arg1.getMessage());

arg1.printStackTrace();

}

});

}

/**

* post请求 map为body

*

* @param url

* @param map

* @param callback

*/

public void post(String url, Map map,final HttpCallback callback) {

/**

* 创建请求的参数body

*/

FormBody.Builder builder = new FormBody.Builder();

/**

* 遍历key

*/

if (null != map) {

for (Map.Entry entry : map.entrySet()) {

builder.add(entry.getKey(), entry.getValue().toString());

}

}

RequestBody body = builder.build();

Request request = new Request.Builder().url(url).post(body).build();

client.newCall(request).enqueue(new Callback() {

@Override

public void onResponse(final Response response) throws IOException {

// TODO Auto-generated method stub

if (response.isSuccessful()) {

onSuccess(callback, response.body().string());

} else {

onError(callback, response.message());

}

}

@Override

public void onFailure(Request arg0, IOException arg1) {

// TODO Auto-generated method stub

arg1.printStackTrace();

onError(callback, arg1.getMessage());

}

});

}

/**

* get请求

* @param url

* @param callback

*/

public void get(String url, final HttpCallback callback) {

Request request = new Request.Builder().url(url).build();

client.newCall(request).enqueue(new Callback() {

@Override

public void onResponse(Response response) throws IOException {

// TODO Auto-generated method stub

if (response.isSuccessful()) {

onSuccess(callback, response.body().string());

} else {

onError(callback, response.message());

}

}

@Override

public void onFailure(Request arg0, IOException arg1) {

// TODO Auto-generated method stub

onError(callback, arg1.getMessage());

arg1.printStackTrace();

}

});

}

private void onSuccess(final HttpCallback callback, final String data) {

debug(data);

if (null != callback) {

handler.post(new Runnable() {

public void run() {

callback.onSuccess(data);

}

});

}

}

private void onError(final HttpCallback callback,final String msg) {

if (null != callback) {

handler.post(new Runnable() {

public void run() {

callback.onError(msg);

}

});

}

}

/**

* http请求回调

*

* @author Flyjun

*

*/

public static abstract class HttpCallback {

// 成功回调

public abstract void onSuccess(String data);

// 失败回调

public void onError(String msg) {};

}

}

 类似资料: