Android的SDK天青 - 当我尝试加载从Blob存储东西的NullPointerException [英] Android Azure SDK - NullPointerException when I try to load something from blob storage
问题描述
我创建在Azure上的容器被称为assetcontainer。
I've created a container on Azure called 'assetcontainer'.
我已经按照样品创建以下code:
I've followed the sample to create the following code:
CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString);
mClient = account.createCloudBlobClient();
mAssetContainer = mClient.getContainerReference("assetcontainer");
// Download the blob
// For each item in the container
for (ListBlobItem blobItem : mAssetContainer.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
try {
retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName()));
}
catch(Exception ex){}
}
}
和我收到以下异常,当我尝试遍历listBlobs():
And I am receiving the following exception when I try to iterate the listBlobs():
Process: com.training.app.debug, PID: 3284
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.training.app.debug/com.training.app.activities.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:783)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:145)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:252)
at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:109)
at com.training.azure.AzureStorage.syncAssets(AzureStorage.java:57)
at com.training.app.activities.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
我不知道这可能是错在这里...这是湛蓝的SDK中的错误?
I'm not sure what could be wrong here... Is this a bug with the azure sdk?
推荐答案
我发现从的此页面,它为我工作。
I found a solution from this page, and it worked for me.
Android的不允许在主线程中执行网络操作,所以你需要在一个异步任务执行code,是这样的:
Android doesn't allow performing network operations in the main thread, so you need to execute the code in an asynchronous task, like this:
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString);
mClient = account.createCloudBlobClient();
mAssetContainer = mClient.getContainerReference("assetcontainer");
// Download the blob
// For each item in the container
for (ListBlobItem blobItem : mAssetContainer.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
try {
retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName()));
}
catch(Exception ex){}
}
}
}
}
然后调用新MyTask()的execute()
在主线程。
这篇关于Android的SDK天青 - 当我尝试加载从Blob存储东西的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!