gpt4 book ai didi

android - 尝试上传到 Dropbox : NetworkOnMainThreadException?

转载 作者:太空狗 更新时间:2023-10-29 12:52:27 25 4
gpt4 key购买 nike

我现在已经为此奋斗了大约 6 个小时。我遵循了 Dropbox 的“教程”(如果他们可以这样称呼,因为他们非常糟糕),玩过 DBRoulette 示例,并做了很多事情来让我的应用程序正常工作。

我的应用程序可以毫无问题地进行身份验证。但是我无法上传任何内容,尽管完全按照教程中的内容进行操作:

这是我的一小段代码(这是为了将创建的笔记保存在手机上,然后上传到 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,我不知道为什么。我正在尝试按照标题为“上传文件”的部分进行操作 here .他们的代码 fragment 让我感到困惑的是,他们甚至没有试图捕捉我抛出的异常......

有什么帮助吗?我真的需要在下周五完成这项工作。

最佳答案

在Honeycomb SDK之前,允许在主线程进行网络操作。但是,对于 Honeycomb,它不再被允许并且 NetworkOnMainThreadException如果在主/UI 线程中尝试网络操作,则抛出。

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

关于android - 尝试上传到 Dropbox : NetworkOnMainThreadException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10892858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com