RNFetchBlob无法下载apk,但是在浏览器中,URL可以下载文件

时间:2019-11-11 05:18:10

标签: android react-native okhttp

我开发了一个本机应用程序,对于android,我使用RNFetchBlob下载APK。在Android中,RNFetchBlob使用OkHttp库下载文件。 但是APK无法下载。当我将链接放在android中时,它可以 成功下载文件。

我尝试用其他URL替换它,RNFetchBlob可以下载文件。

但是我的网址是一个简单的网址:http://host:port/AppDownLoad/publish/some.apk

RNFetchBlob配置如下:

 RNFetchBlob.config({
      path: dirs.DCIMDir + "/Some.apk",
      timeout:180000,
    })
      .fetch("GET", APK_URL)
      .progress({ count: 10 }, (received, total) => {
        let currentProgress = received / total;
      })
      .then(res => {

        if(res.respInfo.timeout){
          Linking.openURL(APKURL)
          return;
        }
        android.actionViewIntent(
         res.path(),
        "application/vnd.android.package-archive"
        );
      })
      .catch(error => {
        console.log(error); 
        // Linking.openURL(APKURL)
      });

第三个库OkHttp源代码是:

OkHttpClient.Builder clientBuilder;
if (this.options.trusty) {
                clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient(client);
            } else {
                clientBuilder = client.newBuilder();
            }

            final Request.Builder builder = new Request.Builder();
            try {
                builder.url(new URL(url));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            builder.method("GET", null);
            final Request req = builder.build();
            clientBuilder.connectionPool(pool);
            clientBuilder.retryOnConnectionFailure(false);
            clientBuilder.followRedirects(options.followRedirect);
            clientBuilder.followSslRedirects(options.followRedirect);
            clientBuilder.retryOnConnectionFailure(true);

            OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();

            Call call =  client.newCall(req);
            taskTable.put(taskId, call);
            call.enqueue(new okhttp3.Callback(),{callback}


错误信息为the content-length is disagreed with the stream,谁能告诉我为什么?

0 个答案:

没有答案