2

我遇到了很多例外,比如 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;
}

}

4

4 回答 4

3

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。ui 应该在主 ui 线程上更新。

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

于 2013-04-22T13:01:31.010 回答
3

看看这篇文章 - https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
从 Android 4.0 开始,所有网络请求都应该在单独的线程中执行,否则会发生异常。

于 2013-04-22T12:55:08.503 回答
0

Android 3.0 以后的主 UI 线程不支持网络调用。您可以使用 Thread 或 AsyncTask 来实现网络调用。

谢谢

于 2013-04-22T13:17:56.873 回答
0

不建议这样做,但有帮助..

if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
于 2013-10-02T13:22:19.350 回答