public static void callAll() {
mOkHttpClient.dispatcher().cancelAll();
}
public static void init(OkHttpClient okHttpClient) {
mOkHttpClient = okHttpClient;
}
@Override
public void get(Context context, String url, Map
Map
String jointUrl = URLUtil.jointParams(url, params);
Request.Builder requestBuilder = new Request.Builder()
.url(jointUrl)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
callBack.onBefore(context);
Request request = requestBuilder.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
});
}
@Override
public void post(Context context, String url, Map
final EngineCallBack callBack) {
RequestBody requestBody = appendBody(params);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request request = requestBuilder
.post(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(
new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
}
);
}
@Override
public void put(Context context, String url, Map
RequestBody requestBody = appendBody(params);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request request = requestBuilder
.put(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(
new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
}
);
}
@Override
public void delete(Context context, String url, Map
RequestBody requestBody = appendBody(params);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request request = requestBuilder
.delete(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(
new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
}
);
}
@Override
public void post(Context context, String url, Map
MediaType mediaType = MediaType.parse(“application/xml; charset=utf-8”);//“类型,字节码”
RequestBody requestBody = RequestBody.create(mediaType, json);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request
request = requestBuilder
.post(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
});
}
@Override
public void put(Context context, String url, Map
MediaType mediaType = MediaType.parse(“application/json; charset=utf-8”);//“类型,字节码”
RequestBody requestBody = RequestBody.create(mediaType, json);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request request = requestBuilder
.put(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
});
}
@Override
public void delete(Context context, String url, Map
MediaType mediaType = MediaType.parse(“application/json; charset=utf-8”);//“类型,字节码”
RequestBody requestBody = RequestBody.create(mediaType, json);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.tag(context);
if (header != null && header.size() > 0) {
for (Map.Entry
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
Request request = requestBuilder
.delete(requestBody)
.build();
callBack.onBefore(context);
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onAfter();
callBack.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onAfter();
ResponseBody responseBody = response.body();
callBack.onSuccess(responseBody);
}
});
}
/**
组装post请求参数body
@param params
@return
*/
private RequestBody appendBody(Map
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
addParams(builder, params);
return builder.build();
}
/**
添加参数
判断是否能上传文件
*/
private void addParams(MultipartBody.Builder builder, Map
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object value = params.get(key);
if (value instanceof File) {
File file = (File) value;
builder.addFormDataPart(key, file.getName(),
RequestBody.create(MediaType
.parse(guessMineType(file.getAbsolutePath())),
file));
} else if (value instanceof List) {
//代表提交的是List集合
try {
List fileList = (List) value;
for (int i = 0; i < fileList.size(); i++) {
//获取文件
File file = fileList.get(i);
builder.addFormDataPart(key + i, file.getName(),
RequestBody.create(MediaType
.parse(guessMineType(file.getAbsolutePath())),
file));
}
} catch (Exception e) {
e.printStackTrace();
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊
le));
} else if (value instanceof List) {
//代表提交的是List集合
try {
List fileList = (List) value;
for (int i = 0; i < fileList.size(); i++) {
//获取文件
File file = fileList.get(i);
builder.addFormDataPart(key + i, file.getName(),
RequestBody.create(MediaType
.parse(guessMineType(file.getAbsolutePath())),
file));
}
} catch (Exception e) {
e.printStackTrace();
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊