Update v1.2.2版本的 MD5 值为:87ab33f0c8bd4c1dd345062aa2794e73
以下内容为反编译后的 OkHttpUtil.java 源代码,内容仅作参考
package com.luxcine.luxcine_ota.version;
import android.util.Log;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class OkHttpUtil {
public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
private static final String TAG = "OkHttpUtil";
String str = null;
private OkHttpClient mOkHttpClient = new OkHttpClient();
public interface OnOkHttpListener {
void onFailure(Exception e);
void onSuccess(String response);
}
public void getAsynHttp(String url, final OnOkHttpListener listener) {
Request.Builder requestBuilder = new Request.Builder().url(url);
requestBuilder.method("GET", null);
Request request = requestBuilder.build();
Call mCall = this.mOkHttpClient.newCall(request);
mCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(OkHttpUtil.TAG, "onFailure: ----------getAsynHttp---------" + e.toString());
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.cacheResponse().toString();
Log.e(OkHttpUtil.TAG, "cache---------------------" + response);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
}
});
}
public void getAsynHttpAccessToken(String url, final OnOkHttpListener listener) {
Request.Builder requestBuilder = new Request.Builder().url(url).header("X-Renmaituan-Business-App-Name", "BUSINESS_CARD");
requestBuilder.method("GET", null);
Request request = requestBuilder.build();
Call mCall = this.mOkHttpClient.newCall(request);
mCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(OkHttpUtil.TAG, "onFailure: -------------------");
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.cacheResponse().toString();
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
}
});
}
public void getAsynHttpAccessTokenMap(String url, Map<String, String> map, final OnOkHttpListener listener) {
Iterator<String> keys = map.keySet().iterator();
Iterator<String> values = map.values().iterator();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("?");
for (int i = 0; i < map.size(); i++) {
String value = null;
try {
value = URLEncoder.encode(values.next(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
stringBuffer.append(keys.next() + "=" + value);
if (i != map.size() - 1) {
stringBuffer.append("&");
}
}
Log.e(TAG, "getAsynHttpAccessTokenMap: ----------------" + url + stringBuffer.toString());
StringBuilder sb = new StringBuilder();
sb.append(url);
sb.append(stringBuffer.toString());
Request.Builder requestBuilder = new Request.Builder().url(sb.toString()).header("X-Renmaituan-Business-App-Name", "BUSINESS_CARD");
requestBuilder.method("GET", null);
Request request = requestBuilder.build();
Call mCall = this.mOkHttpClient.newCall(request);
mCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e2) {
Log.e(OkHttpUtil.TAG, "onFailure: -------------------");
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e2);
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.cacheResponse().toString();
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
}
});
}
public void postAsynHttp(String url, Map<String, String> paramsMap, final OnOkHttpListener listener) {
if (paramsMap.size() > 0) {
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
String value = paramsMap.get(key);
builder.add(key, value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).header("X-Renmaituan-Business-App-Name", "BUSINESS_CARD").post(formBody).build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
if (response.code() == 200) {
Log.e(OkHttpUtil.TAG, "success: ------------------------200");
} else if (response.code() == 401) {
Log.e(OkHttpUtil.TAG, "success: -------------没有权限-----------401");
} else if (response.code() == 400) {
Log.e(OkHttpUtil.TAG, "success: ------------------------400");
} else if (response.code() == 500) {
Log.e(OkHttpUtil.TAG, "success: -----------服务器异常,请稍后再试-------------500");
}
}
});
}
}
public void postAsynHttpAccessToken(String url, Map<String, String> paramsMap, final OnOkHttpListener listener) {
if (paramsMap.size() > 0) {
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
String value = paramsMap.get(key);
builder.add(key, value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).header("X-Renmaituan-Business-App-Name", "BUSINESS_CARD").post(formBody).build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
Log.e(OkHttpUtil.TAG, "onFailure: ------------------" + e.toString());
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
if (response.code() == 200) {
Log.e(OkHttpUtil.TAG, "success: ------------------------200");
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
} else if (response.code() == 401) {
Log.e(OkHttpUtil.TAG, "success: -------------没有权限-----------401");
} else if (response.code() == 400) {
Log.e(OkHttpUtil.TAG, "success: -------------参数错误-----------400");
} else if (response.code() == 500) {
Log.e(OkHttpUtil.TAG, "success: -----------服务器异常,请稍后再试-------------500");
}
}
});
}
}
public void postAsynHttpAccessTokenJson(String url, Map<String, String> map, final OnOkHttpListener listener) {
Gson mGson = new Gson();
String params = mGson.toJson(map);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, params);
Request request = new Request.Builder().url(url).post(body).build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
if (response.code() == 200) {
Log.e(OkHttpUtil.TAG, "success: ------------------------200");
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
Log.e(OkHttpUtil.TAG, "onResponse: ----OkHttpUtil---" + str);
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
} else if (response.code() == 401) {
Log.e(OkHttpUtil.TAG, "success: -------------没有权限-----------401");
} else if (response.code() == 400) {
Log.e(OkHttpUtil.TAG, "success: -------------参数错误-----------400");
} else if (response.code() == 500) {
Log.e(OkHttpUtil.TAG, "success: -----------服务器异常,请稍后再试-------------500");
}
}
});
}
public void postAsynHttpToken(String url, Map<String, String> map, final OnOkHttpListener listener) {
if (map.size() > 0) {
String access_token = null;
for (String key : map.keySet()) {
String access_token2 = map.get(key);
access_token = access_token2;
}
Request request = new Request.Builder().url(url).header("Authorization", access_token).post(RequestBody.create((MediaType) null, "")).build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
}
});
}
}
public void putAsynHttpAccessToken(String url, Map<String, String> paramsMap, final OnOkHttpListener listener) {
if (paramsMap.size() > 0) {
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
String value = paramsMap.get(key);
builder.add(key, value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).header("X-Renmaituan-Business-App-Name", "BUSINESS_CARD").put(formBody).build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
Log.e(OkHttpUtil.TAG, "onFailure: ------------------" + e.toString());
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
if (response.code() == 200) {
Log.e(OkHttpUtil.TAG, "success: ------------------------200");
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
} else if (response.code() == 401) {
Log.e(OkHttpUtil.TAG, "success: -------------没有权限-----------401");
} else if (response.code() == 400) {
Log.e(OkHttpUtil.TAG, "success: -------------参数错误-----------400");
} else if (response.code() == 500) {
Log.e(OkHttpUtil.TAG, "success: -----------服务器异常,请稍后再试-------------500");
}
}
});
}
}
public void doDelete(String url, final OnOkHttpListener listener) {
FormBody formBody = new FormBody.Builder().build();
Request.Builder builder = new Request.Builder().url(url).delete(formBody);
Request request = builder.build();
Call call = this.mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call2, IOException e) {
Log.e(OkHttpUtil.TAG, "删除失败onFailure:------ " + e.getMessage());
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onFailure(e);
}
}
@Override
public void onResponse(Call call2, Response response) throws IOException {
ResponseBody body = response.body();
String s = body.toString();
Log.e(OkHttpUtil.TAG, "删除成功onResponse---------: " + s);
if (response.cacheResponse() != null) {
OkHttpUtil.this.str = response.toString();
Log.e(OkHttpUtil.TAG, "cache---" + OkHttpUtil.this.str);
return;
}
String str = response.body().string();
OnOkHttpListener onOkHttpListener = listener;
if (onOkHttpListener != null) {
onOkHttpListener.onSuccess(str);
}
}
});
}
private void postAsynFile() {
File file = new File("/sdcard/wangshu.txt");
Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)).build();
this.mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e(OkHttpUtil.TAG, response.body().string());
}
});
}
private void downAsynFile() {
Request request = new Request.Builder().url("http://img.my.csdn.net/uploads/201603/26/1458988468_5804.jpg").build();
this.mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) {
InputStream inputStream = response.body().byteStream();
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File("/sdcard/wangshu.jpg"));
byte[] buffer = new byte[2048];
while (true) {
int len = inputStream.read(buffer);
if (len == -1) {
break;
}
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
} catch (IOException e) {
Log.e(OkHttpUtil.TAG, "IOException");
e.printStackTrace();
}
Log.e(OkHttpUtil.TAG, "文件下载成功");
}
});
}
private void sendMultipart() {
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("title", "wangshu").addFormDataPart("image", "wangshu.jpg", RequestBody.create(MEDIA_TYPE_PNG, new File("/sdcard/wangshu.jpg"))).build();
Request request = new Request.Builder().header("Authorization", "Client-ID ...").url("https://api.imgur.com/3/image").post(requestBody).build();
this.mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e(OkHttpUtil.TAG, response.body().string());
}
});
}
}