Android:java.io.IOException:null SSL指针

时间:2012-06-16 06:42:27

标签: android amazon-s3

在我的Android应用程序中,我必须从亚马逊存储桶下载视频。当我尝试从亚马逊存储桶中下载视频时,经过一段时间后会出现错误,如

06-16 12:01:55.792: I/System.out(577): ERROR in Thread : java.io.IOException: null SSL pointer

此处为亚马逊s3存储桶下载视频代码

public class AmazonS3Helper 
{
GeneralHelper objHelper;
Context myContext;
String bucketName;
String accessKey;
String secretKey;

ProgressBar myProgressBar;

AWSCredentials credential;

int len;
int total = 0;

public AmazonS3Helper(Context context,List list)
{
    try
    {
        myContext = context;
        objHelper = new GeneralHelper(myContext);

        bucketName = "mybucketName";
        accessKey = "myaccesskey";
        secretKey = "mysecretkey";

        credential = new BasicAWSCredentials(accessKey, secretKey);

        getFileFromAmazonS3(list);
    }
    catch (Exception ex) 
    {
        System.out.println("ERROR : "+ex.toString());
    }
}

public AmazonS3Helper(Context context,String amazonObjectName,ProgressBar progress)
{
    try
    {
        myContext = context;
        objHelper = new GeneralHelper(myContext);
        //progressBar = progress;

        bucketName = "mybucketname";
        accessKey = "key";
        secretKey = "key";

        credential = new BasicAWSCredentials(accessKey, secretKey);

        getFileFromAmazonS3(amazonObjectName,progress);
    }
    catch (Exception ex) 
    {
        System.out.println("ERROR : "+ex.toString());
    }
}

public void getFileFromAmazonS3(List list)
{
    try
    {
        List<DownloadItemList> downloadList = list;

        List<S3Object> s3ObjectList = new ArrayList<S3Object>();

        for(int i=0;i<downloadList.size();i++)
        {
            DownloadItemList downloadItem = downloadList.get(i);

            String objectImageKey = downloadItem.getEncryptName()+".app/"+downloadItem.getImageName();
            String objectAudioKey = downloadItem.getEncryptName()+".app/"+downloadItem.getAudioName();

            GetObjectRequest objRequest = new GetObjectRequest(bucketName,objectImageKey);
            AmazonS3Client myS3Client = new AmazonS3Client(credential);
            S3Object s3Object = myS3Client.getObject(objRequest);

            s3ObjectList.add(s3Object);             

            objRequest = new GetObjectRequest(bucketName,objectImageKey);
            myS3Client = new AmazonS3Client(credential);
            s3Object = myS3Client.getObject(objRequest);

            s3ObjectList.add(s3Object);
        }
    }
    catch (Exception ex) 
    {
        System.out.println("ERROR : "+ex.toString());
    }
}


// For Video
public void getFileFromAmazonS3(String strObjectName,ProgressBar progress)
{
    try
    {   
                    String md5Name = objHelper.getMD5HasCode(strObjectName);

        String strObjectKey = "video path";

        GetObjectRequest objRequest = new GetObjectRequest(bucketName,strObjectKey);

        AmazonS3Client myS3Client = new AmazonS3Client(credential);

        S3Object s3Object = myS3Client.getObject(objRequest);

        String filePath = strObjectName+"_video.mp4";

        System.out.println("OK - 8");

        downloadObject(s3Object,filePath,progress);         


    }
    catch (Exception ex) 
    {
        System.out.println("ERROR : "+ex.toString());
    }
}

private void downloadObject(S3Object s3Object,String path,ProgressBar progress)
{
    try
    {
        myProgressBar = progress;

        System.out.println("OK - 8");

        String filePath = objHelper.getFullFilePath(path); // + "noha_video1.mp4";

        System.out.println("FilePath : "+filePath);

        final long fileSize = s3Object.getObjectMetadata().getContentLength();
        final InputStream input = s3Object.getObjectContent();
        final FileOutputStream fos = new FileOutputStream(filePath);

        myProgressBar.setMax((int) fileSize);

        System.out.println("Input Stream Length : " + fileSize);

        final byte[] buf = new byte[1024];

        Thread thread = new Thread()
        {
            public void run()
            {
                try
                {
                    total = 0;

                    while((len = input.read(buf)) > 0)
                    {
                        fos.write(buf,0,len);

                        total = total + len;

                        Message msg = handler.obtainMessage();
                        Bundle b = new Bundle();
                        b.putInt("total", total);
                        msg.setData(b);
                        handler.sendMessage(msg);

                    }
                    if(len == -1 && total==Integer.parseInt(String.valueOf(fileSize)))
                    {
                        Thread.currentThread().interrupt();

                        input.close();
                        fos.close();

                        objHelper.startTargetActivity(myContext, VideoActivity.class);
                    }
                }
                catch (Exception ex) 
                {
                    System.out.println("ERROR in Thread : "+ex.toString());
                }
            }
        };

        thread.start();
    }
    catch (Exception ex) 
    {
        System.out.println("ERROR in Run Thread : "+ex.toString());
    }
}

Handler handler=new Handler() 
{

    @Override

    public void handleMessage(Message msg) 
    {
        myProgressBar.setProgress(msg.getData().getInt("total"));

    }

};

}

1 个答案:

答案 0 :(得分:0)

您可以检查最终为获取文件生成的网址,在浏览器和网址中粘贴相同的网址有时当您'http'发布时'https' 'http',如果'https'转换为'https'那么您必须获得证书。