android - Trying To Upload To Dropbox: NetworkOnMainThreadException? -
i have been fighting 6 hours now. have followed dropbox's "tutorials" (if can called that, because awfully poor), played dbroulette example, , done tons of stuff app working.
my app can authenticate no problems. can't upload despite doing tutorial doing:
this little snippet of code have (this save created note on phone , upload dropbox):
savebtn.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { try { //create directory if doesn't exist. if exist next 2 line won't anything. file dropnotesdir = new file(environment.getexternalstoragedirectory() + "/documents/appdata/dropnotes/"); dropnotesdir.mkdirs(); //----------------------------------------------------------------------------------------------- string wholenotestring = notebody.gettext().tostring(); //now create title of txt file. first line of whole note. name truncated 32 characters //if it's long. string notetitle; if(wholenotestring.indexof("\n") >= 0) //new line character found. { notetitle = wholenotestring.substring(0, wholenotestring.indexof("\n")); if(notetitle.length() >= 32) { notetitle = wholenotestring.substring(0, 32); } } else { if(wholenotestring.length() >= 32) { notetitle = wholenotestring.substring(0, 32); }else { notetitle = wholenotestring; } } if(extras.getstring("file-mode").equals("modify")) { //we need delete existing file if exist save new one. file existing = new file(environment.getexternalstoragedirectory() + "/documents/appdata/dropnotes/" + extras.getstring("notetitle")); existing.delete(); } file newnote = new file(environment.getexternalstoragedirectory() + "/documents/appdata/dropnotes/" + notetitle + ".txt"); printwriter newnotewriter = new printwriter(new fileoutputstream(newnote)); newnotewriter.print(notebody.gettext().tostring()); newnotewriter.close(); //trying upload dropbox here file filetoupload = new file(environment.getexternalstoragedirectory() + "/documents/appdata/dropnotes/" + notetitle + ".txt"); fileinputstream file2uis = new fileinputstream(filetoupload); entry newentry = mdbapi.putfile("/" + notetitle + ".txt", file2uis, filetoupload.length(), null, null); //end of trying upload dropbox here toast.maketext(view.getcontext(), "saved successfully", toast.length_short).show(); finish(); } catch (filenotfoundexception e) { toast.maketext(view.getcontext(), "file not found: " + e.getmessage(), toast.length_short).show(); e.printstacktrace(); } catch(exception e) { toast.maketext(view.getcontext(), "some bizarre exception occured: " + e.getclass().tostring(), toast.length_short).show(); e.printstacktrace(); } } }); it's giving me networkonmainthreadexception , don't know why. i'm trying follow section titled "uploading file" here. baffles me snippet not trying catch exception i'm getting thrown at...
any help? need working next friday.
prior honeycomb sdk, allowed perform network operation on main thread. however, honeycomb, no longer allowed , networkonmainthreadexception thrown if network operation attempted in main/ui thread.
you need perform network operations in different thread. can take @ asynctask achieve same.
Comments
Post a Comment