Search in sources :

Example 11 with Response

use of com.ecwid.consul.v1.Response in project okhttp-OkGo by jeasonlzy.

the class DownloadTask method doInBackground.

/** 一旦该方法执行,意味着开始下载了 */
@Override
protected DownloadInfo doInBackground(Void... params) {
    if (isCancelled())
        return mDownloadInfo;
    mPreviousTime = System.currentTimeMillis();
    mDownloadInfo.setNetworkSpeed(0);
    mDownloadInfo.setState(DownloadManager.DOWNLOADING);
    postMessage(null, null);
    long startPos = mDownloadInfo.getDownloadLength();
    Response response;
    try {
        response = mDownloadInfo.getRequest().headers("RANGE", "bytes=" + startPos + "-").execute();
    } catch (IOException e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("网络异常", e);
        return mDownloadInfo;
    }
    int code = response.code();
    if (code == 404 || code >= 500) {
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("服务器数据错误", null);
        return mDownloadInfo;
    }
    //构建下载文件路径,如果有设置,就用设置的,否者就自己创建
    String url = mDownloadInfo.getUrl();
    String fileName = mDownloadInfo.getFileName();
    if (TextUtils.isEmpty(fileName)) {
        fileName = HttpUtils.getNetFileName(response, url);
        mDownloadInfo.setFileName(fileName);
    }
    if (TextUtils.isEmpty(mDownloadInfo.getTargetPath())) {
        File targetFolder = new File(mDownloadInfo.getTargetFolder());
        if (!targetFolder.exists())
            targetFolder.mkdirs();
        File file = new File(targetFolder, fileName);
        mDownloadInfo.setTargetPath(file.getAbsolutePath());
    }
    //检查文件有效性,文件大小大于总文件大小
    if (startPos > mDownloadInfo.getTotalLength()) {
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("断点文件异常,需要删除后重新下载", null);
        return mDownloadInfo;
    }
    if (startPos == mDownloadInfo.getTotalLength() && startPos > 0) {
        mDownloadInfo.setProgress(1.0f);
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.FINISH);
        postMessage(null, null);
        return mDownloadInfo;
    }
    //设置断点写文件
    File file = new File(mDownloadInfo.getTargetPath());
    ProgressRandomAccessFile randomAccessFile;
    try {
        randomAccessFile = new ProgressRandomAccessFile(file, "rw", startPos);
        randomAccessFile.seek(startPos);
    } catch (Exception e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("没有找到已存在的断点文件", e);
        return mDownloadInfo;
    }
    //获取流对象,准备进行读写文件
    long totalLength = response.body().contentLength();
    if (mDownloadInfo.getTotalLength() == 0) {
        mDownloadInfo.setTotalLength(totalLength);
    }
    InputStream is = response.body().byteStream();
    //读写文件流
    try {
        download(is, randomAccessFile);
    } catch (IOException e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("文件读写异常", e);
        return mDownloadInfo;
    }
    //循环结束走到这里,a.下载完成     b.暂停      c.判断是否下载出错
    if (isCancelled()) {
        mDownloadInfo.setNetworkSpeed(0);
        if (//暂停
        isPause)
            //暂停
            mDownloadInfo.setState(DownloadManager.PAUSE);
        else
            //停止
            mDownloadInfo.setState(DownloadManager.NONE);
        postMessage(null, null);
    } else if (file.length() == mDownloadInfo.getTotalLength() && mDownloadInfo.getState() == DownloadManager.DOWNLOADING) {
        mDownloadInfo.setNetworkSpeed(0);
        //下载完成
        mDownloadInfo.setState(DownloadManager.FINISH);
        postMessage(null, null);
    } else if (file.length() != mDownloadInfo.getDownloadLength()) {
        mDownloadInfo.setNetworkSpeed(0);
        //由于不明原因,文件保存有误
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("未知原因", null);
    }
    return mDownloadInfo;
}
Also used : Response(okhttp3.Response) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 12 with Response

use of com.ecwid.consul.v1.Response in project okhttp-OkGo by jeasonlzy.

the class UploadTask method doInBackground.

/** 一旦该方法执行,意味着开始下载了 */
@Override
protected UploadInfo doInBackground(Void... params) {
    if (isCancelled())
        return mUploadInfo;
    mUploadInfo.setNetworkSpeed(0);
    mUploadInfo.setState(UploadManager.UPLOADING);
    postMessage(null, null, null);
    //构建请求体,默认使用post请求上传
    Response response;
    try {
        response = mUploadInfo.getRequest().setCallback(new MergeListener()).execute();
    } catch (IOException e) {
        e.printStackTrace();
        mUploadInfo.setNetworkSpeed(0);
        mUploadInfo.setState(UploadManager.ERROR);
        postMessage(null, "网络异常", e);
        return mUploadInfo;
    }
    if (response.isSuccessful()) {
        //解析过程中抛出异常,一般为 json 格式错误,或者数据解析异常
        try {
            T t = (T) mUploadInfo.getListener().parseNetworkResponse(response);
            mUploadInfo.setNetworkSpeed(0);
            //上传成功
            mUploadInfo.setState(UploadManager.FINISH);
            postMessage(t, null, null);
            return mUploadInfo;
        } catch (Exception e) {
            e.printStackTrace();
            mUploadInfo.setNetworkSpeed(0);
            mUploadInfo.setState(UploadManager.ERROR);
            postMessage(null, "解析数据对象出错", e);
            return mUploadInfo;
        }
    } else {
        mUploadInfo.setNetworkSpeed(0);
        mUploadInfo.setState(UploadManager.ERROR);
        postMessage(null, "数据返回失败", null);
        return mUploadInfo;
    }
}
Also used : Response(okhttp3.Response) IOException(java.io.IOException) IOException(java.io.IOException)

Example 13 with Response

use of com.ecwid.consul.v1.Response in project lottie-android by airbnb.

the class AnimationFragment method loadUrl.

private void loadUrl(String url) {
    Request request;
    try {
        request = new Request.Builder().url(url).build();
    } catch (IllegalArgumentException e) {
        onLoadError();
        return;
    }
    if (client == null) {
        client = new OkHttpClient();
    }
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            onLoadError();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                onLoadError();
            }
            try {
                JSONObject json = new JSONObject(response.body().string());
                LottieComposition.Factory.fromJson(getResources(), json, new OnCompositionLoadedListener() {

                    @Override
                    public void onCompositionLoaded(LottieComposition composition) {
                        setComposition(composition, "Network Animation");
                    }
                });
            } catch (JSONException e) {
                onLoadError();
            }
        }
    });
}
Also used : Call(okhttp3.Call) OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 14 with Response

use of com.ecwid.consul.v1.Response in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClient method executeInternal.

@Override
/* package */
ParseHttpResponse executeInternal(ParseHttpRequest parseRequest) throws IOException {
    Request okHttpRequest = getRequest(parseRequest);
    Call okHttpCall = okHttpClient.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    return getResponse(okHttpResponse);
}
Also used : Response(okhttp3.Response) ParseHttpResponse(com.parse.http.ParseHttpResponse) Call(okhttp3.Call) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest)

Example 15 with Response

use of com.ecwid.consul.v1.Response in project sonarqube by SonarSource.

the class HttpHeadersTest method verify_headers_of_js_provided_by_plugins.

@Test
public void verify_headers_of_js_provided_by_plugins() throws Exception {
    Response response = call(orchestrator.getServer().getUrl() + "/static/uiextensionsplugin/extension.js");
    verifySecurityHeaders(response);
    verifyContentType(response, "application/javascript");
}
Also used : Response(okhttp3.Response) Test(org.junit.Test)

Aggregations

Response (okhttp3.Response)1475 Request (okhttp3.Request)1045 IOException (java.io.IOException)547 Test (org.junit.Test)438 Response (retrofit2.Response)338 Call (okhttp3.Call)293 OkHttpClient (okhttp3.OkHttpClient)287 ResponseBody (okhttp3.ResponseBody)245 ServiceResponse (com.microsoft.rest.ServiceResponse)179 RequestBody (okhttp3.RequestBody)153 MockResponse (okhttp3.mockwebserver.MockResponse)148 Callback (okhttp3.Callback)129 List (java.util.List)118 Observable (rx.Observable)114 JSONObject (org.json.JSONObject)93 ArrayList (java.util.ArrayList)90 HttpUrl (okhttp3.HttpUrl)87 ANResponse (com.androidnetworking.common.ANResponse)84 InputStream (java.io.InputStream)81 Map (java.util.Map)81