java azure storage error “enumerating the result”

2019-03-05 02:27发布

问题:

I'm consuming the android azure storage API with the following code:

try
{
    // Retrieve storage account from connection-string.
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.getContainerReference("appstar");

    // Loop over blobs within the container and output the URI to each of them.
    for (ListBlobItem blobItem : container.listBlobs()) {
        System.out.println(blobItem.getUri());
    }

    // hide dat loader
    progress.dismiss();
}
catch (Exception e)
{
    System.out.println("There was a problem");

    // Output the stack trace.
    e.printStackTrace();

    // hide dat loader
    progress.dismiss();
}

but when I get to the for loop, I get the following error:

07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ java.util.NoSuchElementException: An error occurred while enumerating the result, check the original exception for details.
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:113)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at com.k.azurestorage.ConnectionSettingsForm.btn_onCreateSubmitted(ConnectionSettingsForm.java:127)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at android.view.View$1.onClick(View.java:4015)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at android.view.View.performClick(View.java:4780)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
07-23 12:30:46.371    2362-2362/com.k.azurestorage W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ Caused by: com.microsoft.azure.storage.StorageException: Network operations may not be performed on the main thread.
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:224)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:109)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ ... 14 more
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ Caused by: android.os.NetworkOnMainThreadException
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
07-23 12:30:46.372    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
07-23 12:30:46.382    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:125)
07-23 12:30:46.393    2362-2362/com.k.azurestorage W/System.err﹕ ... 15 more

Any Idea what this means?

回答1:

First things first, lets check off the basics that tend to cause odd errors. :)

  1. Could you confirm you're using the Android library and not the Java library?
  2. Could you confirm you have internet permissions in your Android manifest? See our samples for an example.

If neither of these are your issues, then could you do the following?

  1. Post the full exception trace. The iterable API requires us to return a NoSuchElementException, so we wrap the reason the exception actually occurred into that. The full stack trace should give us what we need to debug further.
  2. Post the version of the Android library you're using and the version of Android you're running on. Per the changelog we've had some Android bugs relating to iterables in the past and this will let us confirm you're not hitting those.

Leaving the debugging steps for others, but here the full stack trace gives the answer. In the full strack trace you can see a NetworkOnMainThreadException with the message 'Network operations may not be performed on the main thread.' Because network operations can be slow, Android prevents them from being made on the main thread by default. If you search this error you'll find a variety of examples on how to work with network operations without blocking the UI thread.