Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
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();
}
}
});
}
}
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:
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:
Thank you, it really helped me a lot.
Although this is not specified in the repository: https://github.com/silexcorp/dropbox-sdk-java
Hi there!
If you need more help you can view your support options (expected response time for a 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!