1

我的 Java 应用程序能够连接并获取我的 gmail 联系人,但 Android 应用程序抛出异常“与登录 URI 连接时出错”。

这是两个应用程序的代码。我在模拟器和手机上尝试了 Android 应用程序。在这两种情况下,我都会遇到同样的错误。我可以在两者的网络浏览器中打开页面(我的意思是,我可以访问互联网)。请指教。

//Java Application Code
public static void main(String[] args) throws ServiceException, IOException {
    ContactsService service;
    URL feedUrl;
    ContactFeed resultFeed;
    String url = "https://www.google.com/m8/feeds/" + "contacts/"
            + "username@gmail.com" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("username@gmail.com", "password");
        resultFeed = service.getFeed(feedUrl, ContactFeed.class);
        for (ContactEntry entry : resultFeed.getEntries()) {

            if (entry.getTitle() != null) {

                System.err.println("Contact name: "
                        + entry.getTitle().getPlainText());

            } else {
                System.err.println("Contact has no name");

            }
        }
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    }
}

//Android Application Code
public void onClick(View arg0) {

    ContactsService service;
    URL feedUrl;
    ContactFeed resultFeed;

    String url = "https://www.google.com/m8/feeds/" + "contacts/"
            + "username@gmail.com" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("username@gmail.com", "password");
        resultFeed = service.getFeed(feedUrl, ContactFeed.class);
        for (ContactEntry entry : resultFeed.getEntries()) {

            if (entry.getTitle() != null) {
                Toast.makeText(getApplicationContext(),
                        "Contact name: " + entry.getTitle().getPlainText(),
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(),
                        "Contact has no name", Toast.LENGTH_SHORT).show();

            }

        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        System.out.println("**** Exception: " + e.getMessage());

        //Exception: Error connecting with login URI

    }


}
}
4

2 回答 2

0
 In android emulator you will find email application.You just click that and give the email Id and password.Login in to account.then run your code in emulator it will works
于 2012-01-07T07:32:56.880 回答
0

您确定您可以使用网络连接吗?确保此权限在您的AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.INTERNET" />
于 2012-01-07T02:39:54.497 回答