试图上传到Dropbox的:NetworkOnMainThreadException? [英] Trying To Upload To Dropbox: NetworkOnMainThreadException?

查看:136
本文介绍了试图上传到Dropbox的:NetworkOnMainThreadException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在争取像6小时,这个现在。 我遵循的Dropbox的教程(如果他们可以被称为是,因为他们是非常差),使用了与DBRoulette例子,东西做完吨让我的应用程序的工作。

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:

这是code中的小片段我(这是为了节省手机本身上创建的笔记,然后上传到Dropbox的):

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

    });

它给了我一个NetworkOnMainThreadException,我不知道为什么。我试图按照标题为上传文件这里。是什么令我感到困惑他们的片断是,他们甚至没有试图捕获异常我就要扔在...

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.

推荐答案

此前蜂窝SDK,它允许在主线程上执行网络操作。然而,随着蜂窝,它不再允许和<一href="http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html"><$c$c>NetworkOnMainThreadException如果一个网络操作,试图在主/ UI线程被抛出。

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.

您需要在不同的线程进行网络操作。你可以看看 的AsyncTask 以实现相同

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

这篇关于试图上传到Dropbox的:NetworkOnMainThreadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆