You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
john_son
7 years agoNew member | Level 2
Fatal error on running finishFromCode in Java 3.0.6
Trying to run method finishOAuth of DropboxConnector class (startOAuth has no problems when running.
public DropboxConnector() {
DbxAppInfo appInfo = new DbxAppInfo(DropboxHelper.APP_KEY, DropboxHelper.APP_SECRET);
this.config = new DbxRequestConfig("text-edit/0.1");
this.webAuth = new DbxWebAuth(config, appInfo);
}
public String startOAuth(){
DbxWebAuth.Request authRequest = DbxWebAuth.newRequestBuilder().build();
return webAuth.authorize(authRequest);
}
public String finishOAuth(String code) throws DbxException {
DbxAuthFinish authFinish = webAuth.finishFromCode(code);
return authFinish.getAccessToken();
}
getting Fatal Error on calling finishFromCode(code). Debug shows code is the same I get from dropbox auth page.
Here is the stack:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrew.mywardrobe, PID: 7916
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
at com.dropbox.core.http.StandardHttpRequestor.getOutputStream(StandardHttpRequestor.java:123)
at com.dropbox.core.http.StandardHttpRequestor.access$000(StandardHttpRequestor.java:28)
at com.dropbox.core.http.StandardHttpRequestor$Uploader.<init>(StandardHttpRequestor.java:133)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:72)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:28)
at com.dropbox.core.DbxRequestUtil.startPostRaw(DbxRequestUtil.java:237)
at com.dropbox.core.DbxRequestUtil.startPostNoAuth(DbxRequestUtil.java:216)
at com.dropbox.core.DbxRequestUtil$2.run(DbxRequestUtil.java:453)
at com.dropbox.core.DbxRequestUtil.runAndRetry(DbxRequestUtil.java:498)
at com.dropbox.core.DbxRequestUtil.doPostNoAuth(DbxRequestUtil.java:450)
at com.dropbox.core.DbxWebAuth.finish(DbxWebAuth.java:401)
at com.dropbox.core.DbxWebAuth.finish(DbxWebAuth.java:383)
at com.dropbox.core.DbxWebAuth.finishFromCode(DbxWebAuth.java:295)
at com.example.andrew.mywardrobe.dropbox.DropboxConnector.finishOAuth(DropboxConnector.java:39)
dropbox-core-sdk-3.0.6.jar is added to dependencies.
- You're getting a `NetworkOnMainThreadException`, which means you're trying to make a network call on the main thread, which isn't allowed on Android. (The `finishFromCode` method makes a network call to the Dropbox API servers to exchange the authorization code for an access token.)
You would need to make this call on a background thread instead. There are various examples about how to do this on StackOverflow, e.g.:
https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception
Note that for Android though, the Dropbox Java SDK provides a different app authorization flow anyway, which doesn't use finishFromCode. There's an example Android app here:
https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android
Your AndroidManifest.xml should be set up as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/AndroidManifest.xml#L33
You start the flow by calling startOAuth2Authentication as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/UserActivity.java#L36
You complete the flow by calling getOAuth2Token as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java#L22
Your app can store and re-use the resulting access token for that user, as the example does here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java#L24
- Greg-DBDropbox StaffYou're getting a `NetworkOnMainThreadException`, which means you're trying to make a network call on the main thread, which isn't allowed on Android. (The `finishFromCode` method makes a network call to the Dropbox API servers to exchange the authorization code for an access token.)
You would need to make this call on a background thread instead. There are various examples about how to do this on StackOverflow, e.g.:
https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception
Note that for Android though, the Dropbox Java SDK provides a different app authorization flow anyway, which doesn't use finishFromCode. There's an example Android app here:
https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android
Your AndroidManifest.xml should be set up as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/AndroidManifest.xml#L33
You start the flow by calling startOAuth2Authentication as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/UserActivity.java#L36
You complete the flow by calling getOAuth2Token as shown here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java#L22
Your app can store and re-use the resulting access token for that user, as the example does here:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java#L24- john_sonNew member | Level 2
Thank you, it helps.
The wierd thing is the project was cloned from Windows Android Studio , where the code works without any issues to Macbook one where the issue immediately happened.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,923 PostsLatest Activity: 51 minutes ago
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!