java.net.SocketTimeoutException:读取超时

时间:2015-08-27 22:12:10

标签: java android amazon-web-services amazon-s3 android-asynctask

我连接到AWS(Amazon WebService)并从特定表中读取数据 并且从数据内部S3Link对象使用此对象返回每个项目的图像 某些图像因为SocketTimeoutException

而无法返回
    @Override
    protected List<Item> doInBackground(Void... params) {

        itemList= ItemOperation.getItemList();      // get data from Amazon

        Log.e(TAG,itemList.size()+"");
        return itemList;
    }

    @Override
    protected void onPostExecute(List<Item> items) {
        new DownloadImage().execute(items);
    }
}

public class DownloadImage extends AsyncTask<List<Item>,Void,Void>{
    @Override
    protected Void doInBackground(List<Item>... params) {

        AmazonS3Client s3Client=new AmazonS3Client(Constants.MANAGER.getCredentials());
        InputStream myObjectBytes=null;
        S3Object getResponse=null;
        GetObjectRequest getRequest;
        for (int i=0;i<params[0].size();i++) {
            String bucketName= params[0].get(i).getItemPhoto1().getBucketName();
            String pictureId= params[0].get(i).getItemPhoto1().getKey();
            getRequest = new GetObjectRequest(bucketName,pictureId);
            getResponse = s3Client.getObject(getRequest);
            myObjectBytes = getResponse.getObjectContent();
            itemList.get(i).setBitmap(BitmapFactory.decodeStream(myObjectBytes));
        }
        try {
            myObjectBytes.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

由于java.net.SocketTimeoutException

,不会下载异常和图片
08-28 21:06:20.576      709-755/com.sprintone I/art? Background sticky concurrent mark sweep GC freed 48266(2MB) AllocSpace objects, 16(444KB) LOS objects, 0% free, 21MB/21MB, paused 1.345ms total 103.715ms
08-28 21:07:04.413     709-1541/com.sprintone W/System.err? java.net.SocketTimeoutException: Read timed out
08-28 21:07:04.414     709-1541/com.sprintone W/System.err? at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
08-28 21:07:04.414     709-1541/com.sprintone W/System.err? at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:699)
08-28 21:07:04.414     709-1541/com.sprintone W/System.err? at com.android.okio.Okio$2.read(Okio.java:113)
08-28 21:07:04.414     709-1541/com.sprintone W/System.err? at com.android.okio.RealBufferedSource.read(RealBufferedSource.java:48)
08-28 21:07:04.415     709-1541/com.sprintone W/System.err? at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:446)
08-28 21:07:04.415     709-1541/com.sprintone W/System.err? at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
08-28 21:07:04.415     709-1541/com.sprintone W/System.err? at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
08-28 21:07:04.415     709-1541/com.sprintone W/System.err? at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
08-28 21:07:04.415     709-1541/com.sprintone W/System.err? at java.security.DigestInputStream.read(DigestInputStream.java:113)
08-28 21:07:04.416     709-1541/com.sprintone W/System.err? at com.amazonaws.services.s3.internal.DigestValidationInputStream.read(DigestValidationInputStream.java:60)
08-28 21:07:04.416     709-1541/com.sprintone W/System.err? at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
08-28 21:07:04.416     709-1541/com.sprintone W/System.err? at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-28 21:07:04.416     709-1541/com.sprintone W/System.err? at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
08-28 21:07:04.416     709-1541/com.sprintone W/System.err? at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
08-28 21:07:04.425     709-1541/com.sprintone W/System.err? at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
08-28 21:07:04.431     709-1541/com.sprintone W/System.err? at com.sprintone.userInterface.Fragment.FirstTabFragment$DownloadImage.doInBackground(FirstTabFragment.java:121)
08-28 21:07:04.432     709-1541/com.sprintone W/System.err? at com.sprintone.userInterface.Fragment.FirstTabFragment$DownloadImage.doInBackground(FirstTabFragment.java:107)
08-28 21:07:04.432     709-1541/com.sprintone W/System.err? at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-28 21:07:04.432     709-1541/com.sprintone W/System.err? at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-28 21:07:04.434     709-1541/com.sprintone W/System.err? at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-28 21:07:04.435     709-1541/com.sprintone W/System.err? at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-28 21:07:04.436     709-1541/com.sprintone W/System.err? at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-28 21:07:04.437     709-1541/com.sprintone W/System.err? at java.lang.Thread.run(Thread.java:818)
08-28 21:07:04.437     709-1541/com.sprintone D/skia? ---- read threw an exception
08-28 21:07:04.437     709-1541/com.sprintone D/skia? --- decoder->decode returned false

1 个答案:

答案 0 :(得分:1)

默认连接超时和套接字超时均为15秒。见source on Github。如果您的网络连接相对较差,请考虑通过ClientConfiguration增加超时时间。

ClientConfiguration config = new ClientConfiguration();
config.setConnectionTimeout(60 * 1000); // 60 sec
config.setSocketTimeout(60 * 1000); // 60 sec
AmazonS3Client s3Client = new AmazonS3Client(Constants.MANAGER.getCredentials(), config);