package com.itcast.myimageapp;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class HttpUtil {
private static final String TAG = HttpUtil.class.getCanonicalName();
private HttpUtil() {
}
private static final MediaType TYPE_JSON = MediaType.parse("application/json; chartset=utf-8");
private static final String AUTH = "Authorization";
private static final Handler hadler = new Handler();
private static final OkHttpClient client = new OkHttpClient();
private static String HOST;
private static String TOKEN;
public static void setHOST(String host) {
HttpUtil.HOST = host;
}
public static void setTOKEN(String token) {
HttpUtil.TOKEN = token;
}
public static String getFullPath(String path) {
return HOST + path;
}
public static String parseHtml(String html) {
return html.replaceAll(", " + HOST + "/");
}
public static void get(String path, final CallBack callback) {
Request request = new Request.Builder()
.url(HOST + path)
.get()
.build();
call(callback, request);
}
// 带令牌的get请求
public static void getWithToken(String path, final CallBack callback) {
Request request = new Request.Builder()
.url(HOST + path)
.addHeader(AUTH, TOKEN)
.get()
.build();
call(callback, request);
}
public static void postJSON(String path, Map<String, Object> params, final CallBack callback) {
RequestBody requestBody = RequestBody.create(TYPE_JSON, new Gson().toJson(params));
Request request = new Request.Builder()
.url(HOST + path)
.post(requestBody)
.build();
call(callback, request);
}
// 带令牌的Post请求
public static void postJSONWithToken(String path, Map<String, Object> params, final CallBack callback) {
RequestBody requestBody = RequestBody.create(TYPE_JSON, new Gson().toJson(params));
Request request = new Request.Builder()
.url(HOST + path)
.addHeader(AUTH, TOKEN)
.post(requestBody)
.build();
call(callback, request);
}
// 带令牌的PUT请求
public static void putJSONWithToken(String path, Map<String, Object> params, final CallBack callback) {
RequestBody requestBody = RequestBody.create(TYPE_JSON, new Gson().toJson(params));
Request request = new Request.Builder()
.url(HOST + path)
.addHeader(AUTH, TOKEN)
.put(requestBody)
.build();
call(callback, request);
}
private static void call(final CallBack callback, final Request request) {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
postFailed(callback, 600, e.getMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final int code = response.code();
if (code != 200) {
postFailed(callback, code, response.message());
return;
}
try {
MediaType mediaType = response.body().contentType();
if ("image".equalsIgnoreCase(mediaType.type().toString())) {
InputStream stream = response.body().byteStream();
postSuccessed(callback, stream);
} else {
String pojo_json = response.body().string();
JSONObject result = new JSONObject(pojo_json);
int errCode = result.getInt("code");
if (errCode != 200) {
postFailed(callback, errCode, result.getString("msg"));
} else {
postSuccessed(callback, pojo_json);
}
}
} catch (Exception e) {
postFailed(callback, 700, e.getMessage());
}
}
});
}
private static void postFailed(final CallBack callback, final int errCode, final String msg) {
hadler.post(new Runnable() {
@Override
public void run() {
callback.onFailed(errCode, msg);
}
});
}
private static void postSuccessed(final CallBack callback, final String pojo_json) {
hadler.post(new Runnable() {
@Override
public void run() {
callback.onSuccessed(pojo_json);
}
});
}
private static void postSuccessed(final CallBack callback, final InputStream stream) {
hadler.post(new Runnable() {
@Override
public void run() {
callback.onSuccessed(stream);
}
});
}
private interface ICallBack {
void onFailed(int errCode, String msg);
void onSuccessed(String pojo_json);
void onSuccessed(InputStream stream);
}
public abstract static class CallBack implements ICallBack {
@Override
public void onSuccessed(String pojo_json) {
}
@Override
public void onSuccessed(InputStream stream) {
}
@Override
public void onFailed(int errCode, String msg) {
}
}
}
HttpUtil.get("https://img1.doubanio.com/img/trailer/medium/2892458859.jpg", new HttpUtil.CallBack() {
@Override
public void onSuccessed(InputStream stream) {
super.onSuccessed(stream);
Bitmap bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
}
});
我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
mermaid语法说明 ↩︎