Search in sources :

Example 1 with Dispatcher

use of io.chelizi.amokhttp.Dispatcher in project amhttp by Eddieyuan123.

the class OnAddListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnAddListener.this.getClass());
        T bean = null;
        if (type != null) {
            if (TextUtils.equals(type.toString(), "class java.lang.String"))
                bean = (T) responseStr;
            else
                bean = new Gson().fromJson(responseStr, type);
        }
        final T finalBean = bean;
        Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess(finalBean);
            }
        });
    }
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Example 2 with Dispatcher

use of io.chelizi.amokhttp.Dispatcher in project amhttp by Eddieyuan123.

the class OnUploadListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnUploadListener.this.getClass());
        T bean = null;
        if (type != null) {
            if (TextUtils.equals(type.toString(), "class java.lang.String"))
                bean = (T) responseStr;
            else
                bean = new Gson().fromJson(responseStr, type);
        }
        final T finalBean = bean;
        Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess(finalBean);
            }
        });
    }
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Example 3 with Dispatcher

use of io.chelizi.amokhttp.Dispatcher in project amhttp by Eddieyuan123.

the class OnDownloadListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(final Response response, FileCard fileCard) throws Throwable {
    ResponseBody body = response.body();
    if (body == null) {
        throw new NullPointerException("response body is null");
    } else {
        final Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        InputStream is = body.byteStream();
        long contentLength = body.contentLength();
        final File file = FileUtils.saveFile(is, contentLength, fileCard, new OnSaveListener() {

            @Override
            public void OnProgress(final long progress, final long total) {
                dispatcher.dispatchToUIThread(new Runnable() {

                    @Override
                    public void run() {
                        onProgressChanged(progress, total);
                    }
                });
            }
        });
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess((T) file);
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) Dispatcher(io.chelizi.amokhttp.Dispatcher) File(java.io.File) ResponseBody(okhttp3.ResponseBody)

Example 4 with Dispatcher

use of io.chelizi.amokhttp.Dispatcher in project amhttp by Eddieyuan123.

the class OnFindListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    String responseStr = null;
    if (responseBody != null) {
        responseStr = responseBody.string();
    }
    Type type = ClassUtils.getType(OnFindListener.this.getClass());
    T bean = null;
    if (type != null) {
        if (TextUtils.equals(type.toString(), "class java.lang.String"))
            bean = (T) responseStr;
        else
            bean = new Gson().fromJson(responseStr, type);
    }
    final T finalBean = bean;
    Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
    dispatcher.dispatchToUIThread(new Runnable() {

        @Override
        public void run() {
            onResponseSuccess(finalBean);
        }
    });
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Aggregations

Dispatcher (io.chelizi.amokhttp.Dispatcher)4 ResponseBody (okhttp3.ResponseBody)4 Gson (com.google.gson.Gson)3 Type (java.lang.reflect.Type)3 File (java.io.File)1 InputStream (java.io.InputStream)1