cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Not happy with your Spotify Wrapped? Check out how Kevin on the Dropbox team used Paper to make sure he keeps discovering new music over here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
1
Ask
2
Comments

Fatal error on running finishFromCode in Java 3.0.6

Fatal error on running finishFromCode in Java 3.0.6

john_son
New member | Level 2

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. 

 

1 Accepted Solution

Accepted Solutions

Re: Fatal error on running finishFromCode in Java 3.0.6

Greg-DB
Dropboxer
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.xm...

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/c...

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/c...

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/c...

View solution in original post

2 Replies 2

Re: Fatal error on running finishFromCode in Java 3.0.6

Greg-DB
Dropboxer
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.xm...

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/c...

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/c...

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/c...

Re: Fatal error on running finishFromCode in Java 3.0.6

john_son
New 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.

Who's talking

Top contributors to this post

  • User avatar
    john_son New member | Level 2
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?