OkHttp封装

通讯HttpUtil.java

  • HttpUtil.java
  • 下载并显示图片案例

HttpUtil.java

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编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

  1. mermaid语法说明 ↩︎

你可能感兴趣的:(java,android)