Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

android - Trying To Upload To Dropbox: NetworkOnMainThreadException?

I have been fighting for like 6 hours with this now. I have followed Dropbox's "tutorials" (if they can be called that, because they are awfully poor), played with the DBRoulette example, and done tons of stuff to get my app working.

My app can Authenticate with no problems. But I can't upload anything despite doing exactly what the tutorial is doing:

This is the little snippet of code I have (this is to save a created note on the phone itself and then upload to Dropbox):

        saveBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            try
            {
                //Create the directory if it doesn't exist. If it does exist the next two line won't do anything.
                File dropNotesDir = new File(Environment.getExternalStorageDirectory() + "/documents/AppData/Dropnotes/");
                dropNotesDir.mkdirs();
                //-----------------------------------------------------------------------------------------------
                String wholeNoteString = noteBody.getText().toString();
                //Now we create the title of the txt file. It will be the first line of the whole note. The name will get truncated to 32 characters
                //if it's too long.
                String noteTitle;
                if(wholeNoteString.indexOf("
") >= 0) //New line character found.
                {
                    noteTitle = wholeNoteString.substring(0, wholeNoteString.indexOf("
"));
                    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 will need to delete the existing file if it does exist to save the 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 TO UPLOAD TO 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 TO UPLOAD TO 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 a NetworkOnMainThreadException and I don't know why. I'm trying to follow the section titled "Uploading a File" here. What baffles me about their snippet is that they are not even trying to catch the exception I'm getting thrown at...

Any help? I really need to get this working for next Friday.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Prior to Honeycomb SDK, it was allowed to perform network operation on the main thread. However, with Honeycomb, it is no longer allowed and a NetworkOnMainThreadException is thrown if a network operation is attempted in main/UI thread.

You need to perform network operations in a different thread. You can take a look at AsyncTask to achieve the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...