文档

Android常见问题

更新时间:

本文介绍使用OSS Android SDK的常见问题及解决方法。

说明

使用示例前需要进行HTTPDNS Android SDK接入,具体请参见Android SDK接入

Android SDK是否支持DNS预解析和缓存策略?

以下提供Android结合HTTPDNS SDK以及OkHttp实现DNS预解析和缓存策略的示例。更多信息,请参见Android端HTTPDNS+OkHttp接入指南

  1. 自定义DNS接口

    public class OkHttpDns implements Dns {
        private static final Dns SYSTEM = Dns.SYSTEM;
        HttpDnsService httpdns;
        private static OkHttpDns instance = null;
        private OkHttpDns(Context context) {
            this.httpdns = HttpDns.getService(context, "account id");
        }
        public static OkHttpDns getInstance(Context context) {
            if(instance == null) {
                instance = new OkHttpDns(context);
            }
            return instance;
        }
        @Override
        public List<InetAddress> lookup(String hostname) throws UnknownHostException {
            //通过异步解析接口获取IP地址。
            String ip = httpdns.getIpByHostAsync(hostname);
            if(ip != null) {
                // 如果返回的IP地址不为null,则直接使用该IP地址进行网络请求。
                List<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(ip));
                Log.e("OkHttpDns", "inetAddresses:" + inetAddresses);
                return inetAddresses;
            }
            // 如果返回的IP地址为null,则通过系统DNS服务解析域名。
            return Dns.SYSTEM.lookup(hostname);
        }
    }
  2. 生成okHttpClient实例,并配置到OSS。

    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    
    ClientConfiguration conf = new ClientConfiguration();
    conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒。
    conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒。
    conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个。
    conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次。
    
    OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .dns(OkHttpDns.getInstance(getApplicationContext()));
    // 如果设置了自定义的okHttpClient,ClientConfiguration的部分设置将会失效。您需要将其手动设置到builder上。
    if (conf != null) {
        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(conf.getMaxConcurrentRequest());
    
        builder.connectTimeout(conf.getConnectionTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
                .writeTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
                .followRedirects(conf.isFollowRedirectsEnable())
                .followSslRedirects(conf.isFollowRedirectsEnable())
                .dispatcher(dispatcher);
    
        if (conf.getProxyHost() != null && conf.getProxyPort() != 0) {
            builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(conf.getProxyHost(), conf.getProxyPort())));
        }
    }
    // 仅Android SDK 2.9.12或以上版本支持使用conf.setOkHttpClient();方法。
    conf.setOkHttpClient(builder.build());
    
    OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);

部分文件下载时,进度回调显示totalSize=-1

  • 问题原因

    当文件Content-Type为text/cache-manifest、text/xml、text/plain、text/css、application/javascript、application/x-javascript、application/rss+xml、application/json或者text/json,且文件大于或者等于1 KB时,如果Header中显式设置了Accept-Encoding:gzip,则OSS会返回经过Gzip压缩的数据。下载文件时,即使未设置Accept-Encoding,okhttp也会自动填充为Accept-Encoding:gzip。而经过Gzip压缩后无法得知文件大小,即不会返回Content-Length,因此进度回调显示totalSize=-1。

  • 解决方法

    通过设置Range的方式阻断okhttp自动填充Accept-Encoding:gzip,此时将返回Content-Length,并正常显示进度回调。

    Map<String, String> header = new HashMap<>();
    header.put("x-oss-range-behavior", "standard");
    // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。
    GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
    get.setRange(new Range(0, -1));
    get.setRequestHeaders(header);
    OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
        @Override
        public void onSuccess(GetObjectRequest request, GetObjectResult result) {
            // 请求成功。
            InputStream inputStream = result.getObjectContent();
    
            byte[] buffer = new byte[2048];
            int len;
    
            try {
                while ((len = inputStream.read(buffer)) != -1) {
                    // 处理下载的数据。
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // 请求异常。
            if (clientExcepion != null) {
                // 本地异常如网络异常等。
                clientExcepion.printStackTrace();
            }
            if (serviceException != null) {
                // 服务异常。
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
  • 本页导读 (1)
文档反馈