http://blog.csdn.net/lmj623565791/article/details/47911083 参照了鸿洋_大神的代码重写了自己需要的方法
代码语言:javascript复制 public final static int CONNECT_TIMEOUT = 60;
public final static int READ_TIMEOUT = 100;
public final static int WRITE_TIMEOUT = 60;
private static final OkHttpClient client = new OkHttpClient();
//private static final Handler handler = new Handler();
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static Handler handler = new Handler();
static {
client.newBuilder().readTimeout(READ_TIMEOUT, TimeUnit.SECONDS); // 读取超时
client.newBuilder().writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS); // 写入超时
client.newBuilder().connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS); // 连接超时
}
/**
* 同步get
*
* @param address
* @return
* @throws IOException
*/
public static String syncGet(String address) throws IOException {
Request request = new Request.Builder()
.get()
.url(address)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " response);
}
}
/**
* 同步post
*
* @param address
* @param jsonStr
* @return
* @throws IOException
*/
public static String syncPost(String address, String jsonStr) throws IOException {
RequestBody body = RequestBody.create(JSON, jsonStr);
Request request = new Request.Builder()
.post(body)
.url(address)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " response);
}
}
/**
* 异步get
*
* @param address
* @param callback
*/
public static void asynGet(String address, Callback callback) {
Request request = new Request.Builder()
.get()
.url(address)
.build();
client.newCall(request).enqueue(callback);
}
/**
* 异步post
*
* @param address
* @param jsonStr
* @param callback
*/
public static void asynPost(String address, String jsonStr, Callback callback) {
RequestBody body = RequestBody.create(JSON, jsonStr);
Request request = new Request.Builder()
.post(body)
.url(address)
.build();
client.newCall(request).enqueue(callback);
}
/**
* 异步get 可以访问主线程
* @param address
* @param callback
*/
public static void asynGet(String address, OkhttpUtil.ResultCallback callback) {
Request request = new Request.Builder()
.get()
.url(address)
.build();
deliveryResult(request, callback);
}
/**
* 异步post 可以访问主线程
* @param address
* @param jsonStr
* @param callback
*/
public static void asynPost(String address, String jsonStr, ResultCallback callback) {
RequestBody body = RequestBody.create(JSON, jsonStr);
Request request = new Request.Builder()
.post(body)
.url(address)
.build();
deliveryResult(request, callback);
}
private static void deliveryResult(final Request request, final ResultCallback callback) {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
sendFailedStringCallback(request, e, callback);
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
sendSuccessResultCallback(response.body().string() ,callback);
}
});
}
private static void sendFailedStringCallback(final Request request, final Exception e, final ResultCallback callback)
{
handler.post(new Runnable()
{
@Override
public void run()
{
if (callback != null)
callback.onError(request, e);
}
});
}
private static void sendSuccessResultCallback(final Object object, final ResultCallback callback)
{
handler.post(new Runnable()
{
@Override
public void run()
{
if (callback != null)
{
callback.onResponse(object);
}
}
});
}
public static abstract class ResultCallback<T>
{
public abstract void onError(Request request, Exception e);
public abstract void onResponse(T response);
}
调用:
代码语言:javascript复制// 可以更新线程post请求
OkhttpUtil.asynPost("http://localhost:9090/TomcatTest/test", "{'name':'shaomiao'}", new OkhttpUtil.ResultCallback<String>() {
@Override
public void onError(Request request, Exception e) {
System.out.println("错误了");
}
@Override
public void onResponse(String response) {
System.out.println("返回了");
}
});
// 可以更新线程get请求
OkhttpUtil.asynGet("http://localhost:9090/TomcatTest/test", new OkhttpUtil.ResultCallback() {
@Override
public void onError(Request request, Exception e) {
Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Object response) {
Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
}
});