cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

NetworkOnMainThreadException - connecting to Dropbox account

NetworkOnMainThreadException - connecting to Dropbox account

kestites
Explorer | Level 4
Go to solution

I followed the tutorial on file uploading from the Dropbox website here: https://www.dropbox.com/developers/documentation/java#tutorial 

But I am having problems when running my code. 

Here is my error: 

W/System.err: android.os.NetworkOnMainThreadException
W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
W/System.err: at com.dropbox.core.http.StandardHttpRequestor.getOutputStream(StandardHttpRequestor.java:123)
W/System.err: at com.dropbox.core.http.StandardHttpRequestor.access$000(StandardHttpRequestor.java:28)
W/System.err: at com.dropbox.core.http.StandardHttpRequestor$Uploader.<init>(StandardHttpRequestor.java:133)
W/System.err: at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:72)
W/System.err: at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:28)
W/System.err: at com.dropbox.core.v2.DbxRawClientV2.uploadStyle(DbxRawClientV2.java:221)
W/System.err: at com.dropbox.core.v2.files.DbxUserFilesRequests.upload(DbxUserFilesRequests.java:1261)
W/System.err: at com.dropbox.core.v2.files.UploadBuilder.start(UploadBuilder.java:114)
W/System.err: at com.dropbox.core.v2.files.UploadBuilder.start(UploadBuilder.java:18)
W/System.err: at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
W/System.err: at com.example.kestites.exportdatabase.MainActivity$1.onClick(MainActivity.java:89)
W/System.err: at android.view.View.performClick(View.java:5198)
W/System.err: at android.view.View$PerformClick.run(View.java:21147)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

 

 

Here is my code:

public class MainActivity extends AppCompatActivity {

private Button upload;
private static final String ACCESS_TOKEN = "<APP_KEY>";
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
private String outputFile = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


upload = (Button)findViewById(R.id.button);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {



File patternDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/test.txt");
patternDirectory.mkdirs();
String filename = "test.txt";
outputFile = Environment.getExternalStorageDirectory() + "/test.txt";
String string = "Hello world!";
FileOutputStream outputStream;

try {
outputStream = new FileOutputStream(new File(patternDirectory.getAbsolutePath().toString()+"/test.txt"),true);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
System.out.println("Creating file not working");
e.printStackTrace();
}



try {
InputStream in = new FileInputStream(patternDirectory.getAbsolutePath().toString()+"/test.txt");
FileMetadata metadata = client.files().uploadBuilder("/test.txt").uploadAndFinish(in);

} catch (Exception e) {
System.out.println("Upload file not working");
e.printStackTrace();
}
}
});
}

}
1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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 upload method makes a network call to the Dropbox API servers to send up the file content.) You should make this call on a background thread instead. This isn't specific to Dropbox, so there are several answers about how to do this on StackOverflow, e.g.:

 

https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception

 

There's also an example of uploading in the background in the Android sample app included with the API v2 Java SDK:

 

https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/c...

View solution in original post

3 Replies 3

Rich
Super User II
Go to solution
Moved to the API forum.

Greg-DB
Dropbox Staff
Go to solution

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 upload method makes a network call to the Dropbox API servers to send up the file content.) You should make this call on a background thread instead. This isn't specific to Dropbox, so there are several answers about how to do this on StackOverflow, e.g.:

 

https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception

 

There's also an example of uploading in the background in the Android sample app included with the API v2 Java SDK:

 

https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/c...

silexcorp
Explorer | Level 4
Go to solution

Thank you, it really helped me a lot.

 

Although this is not specified in the repository: https://github.com/silexcorp/dropbox-sdk-java 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    silexcorp Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Rich Super User II
What do Dropbox user levels mean?