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

この記事をご覧ください - https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
Android 4.0 以降、すべてのネットワーク リクエストは別のスレッドで実行する必要があり、そうしないと例外が発生します。

于 2013-04-22T12:55:08.503 に答える
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 に答える