gpt4 book ai didi

android - 为什么我在访问电子表格时遇到网络主线程异常?

转载 作者:太空狗 更新时间:2023-10-29 16:20:32 28 4
gpt4 key购买 nike

我收到很多异常,例如 NetworkMainthread。是的,我已按照 https://developers.google.com/google-apps/spreadsheets/ 中的步骤进行操作

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String USERNAME = "xyz@gmail.com";
String PASSWORD = "rrrrrrr";


URL SPREADSHEET_FEED_URL = null;
try {
SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
/*https://spreadsheets.google.com/feeds/spreadsheets/public/full");*/
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setProtocolVersion(SpreadsheetService.Versions.V3);
try {
service.setUserCredentials(USERNAME, PASSWORD);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


SpreadsheetFeed feed = null;
try {
feed = service.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

List<SpreadsheetEntry> spreadsheets = feed.getEntries();
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());
WorksheetFeed worksheetFeed = null;
try {
worksheetFeed = service.getFeed(
spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ServiceException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);

URL listFeedUrl = worksheet.getListFeedUrl();
ListFeed listFeed = null;
try {
listFeed = service.getFeed(listFeedUrl, ListFeed.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

List<ListEntry> list = listFeed.getEntries();

for (ListEntry row : list) {
System.out.println(row.getTitle().getPlainText() + "\t" + row.getPlainTextContent());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

最佳答案

NetWorkOnMaiNThread 异常发生是因为您试图在主 UI 线程中进行网络相关操作。这是不可能的。

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

如果持续时间很短,您可以使用 asynctask。

http://developer.android.com/reference/android/os/AsyncTask.html

上面链接的引用

AsyncTasks 应该理想地用于短操作(最多几秒钟。)

您可以在 onPreExecute() 中显示进度对话框。在 doInBackground() 中执行网络相关操作。但不要从 doinBackground() 更新 ui(doInbackground() 在后台线程上运行)。在 doInBAckground() 中返回结果。 doInBackground() 的结果是 onPostExecute() 的参数。在 onPostExecute() 中关闭对话框并相应地更新用户界面。

也可以创建一个线程,做网络相关的操作。如果您创建线程,您需要注意不要从线程更新用户界面。 ui 应该在主 ui 线程上更新。

http://developer.android.com/reference/java/util/concurrent/Executor.html

关于android - 为什么我在访问电子表格时遇到网络主线程异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147546/

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