We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

kestites's avatar
kestites
Explorer | Level 4
8 years ago

NetworkOnMainThreadException - connecting to Dropbox account

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();
}
}
});
}

}

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 12 months ago
325 Following

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!