Search in sources :

Example 6 with HttpInfo

use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.

the class MainActivity method async.

/**
     * 异步请求:回调方法可以直接操作UI
     */
private void async() {
    OkHttpUtil.getDefault(this).doGetAsync(HttpInfo.Builder().setUrl(url).addHead("head", //添加头参数
    "test").addParam("param", //添加接口参数
    "test").build(), new Callback() {

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("异步请求失败:" + result);
        }

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("异步请求成功:" + result);
            //GSon解析
            TimeAndDate time = new Gson().fromJson(result, TimeAndDate.class);
            LogUtil.d("MainActivity", time.getResult().toString());
            setFromCacheTV(info);
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) TimeAndDate(com.okhttp3.bean.TimeAndDate) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 7 with HttpInfo

use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.

the class MainActivity method sync.

/**
     * 同步请求:由于不能在UI线程中进行网络请求操作,所以采用子线程方式
     */
private void sync() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            final HttpInfo info = HttpInfo.Builder().setUrl(url).setResponseEncoding(//设置服务器响应编码
            Encoding.UTF_8).build();
            OkHttpUtil.getDefault(this).doGetSync(info);
            final String result = info.getRetDetail();
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    resultTV.setText("同步请求:" + result);
                    setFromCacheTV(info);
                }
            });
        }
    }).start();
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo)

Example 8 with HttpInfo

use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.

the class MainActivity method networkThenCache.

/**
     * 先网络再缓存:先请求网络,失败则请求缓存
     */
private void networkThenCache() {
    OkHttpUtil.Builder().setCacheType(CacheType.NETWORK_THEN_CACHE).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("NETWORK_THEN_CACHE:" + result);
            setFromCacheTV(info);
        }

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            resultTV.setText("NETWORK_THEN_CACHE:" + info.getRetDetail());
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) IOException(java.io.IOException)

Example 9 with HttpInfo

use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.

the class UploadImageActivity method uploadImgOne.

/**
     * 上传第一张图片
     */
private void uploadImgOne() {
    HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("uploadFile", filePathOne, new ProgressCallback() {

        //onProgressMain为UI线程回调,可以直接操作UI
        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            uploadProgressOne.setProgress(percent);
            LogUtil.d(TAG, "上传进度1:" + percent);
        }
    }).build();
    doUploadImg(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Example 10 with HttpInfo

use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.

the class UploadImageActivity method uploadImgMulti.

/**
     * 批量上传:一次性批量上传请使用UploadFileActivity中的doUploadBatch方法
     */
private void uploadImgMulti() {
    HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("file", filePathOne, new ProgressCallback() {

        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            uploadProgressOne.setProgress(percent);
            LogUtil.d(TAG, "上传进度1:" + percent);
        }

        @Override
        public void onResponseMain(String filePath, HttpInfo info) {
            LogUtil.d(TAG, "上传结果1:\n" + filePath + "\n" + info.getRetDetail());
        }
    }).addUploadFile("file", filePathTwo, new ProgressCallback() {

        @Override
        public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
            uploadProgressTwo.setProgress(percent);
            LogUtil.d(TAG, "上传进度2:" + percent);
        }

        @Override
        public void onResponseMain(String filePath, HttpInfo info) {
            LogUtil.d(TAG, "上传结果2:\n" + filePath + "\n" + info.getRetDetail());
        }
    }).build();
    doUploadImg(info);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) ProgressCallback(com.okhttplib.callback.ProgressCallback)

Aggregations

HttpInfo (com.okhttplib.HttpInfo)21 IOException (java.io.IOException)12 ProgressCallback (com.okhttplib.callback.ProgressCallback)10 Callback (com.okhttplib.callback.Callback)6 SocketTimeoutException (java.net.SocketTimeoutException)6 Request (okhttp3.Request)4 DownloadFileInfo (com.okhttplib.bean.DownloadFileInfo)3 SocketException (java.net.SocketException)3 UnknownHostException (java.net.UnknownHostException)3 Call (okhttp3.Call)3 Response (okhttp3.Response)3 NetworkOnMainThreadException (android.os.NetworkOnMainThreadException)2 CallbackMessage (com.okhttplib.bean.CallbackMessage)2 DownloadMessage (com.okhttplib.bean.DownloadMessage)2 UploadMessage (com.okhttplib.bean.UploadMessage)2 BaseCallback (com.okhttplib.callback.BaseCallback)2 ProgressResponseBody (com.okhttplib.progress.ProgressResponseBody)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 OkHttpClient (okhttp3.OkHttpClient)2