无法在 Android 应用程序中获取 Gmail 联系人 - 可以使用相同的代码从桌面 Java 应用程序获取

发布于 2024-12-25 21:54:11 字数 2994 浏览 4 评论 0原文

我的 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/"
            + "[email protected]" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("[email protected]", "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/"
            + "[email protected]" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("[email protected]", "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

    }


}
}

My Java Application is able to connect and fetch my gmail contacts but Android Application is throwing exception "Error connecting with login URI".

Here is the code from both applications. I tried Android App on emulator and Phone. In both, I get the same error. I can open pages in web browser in both (I meant, I have internet access). Please advise.

//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/"
            + "[email protected]" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("[email protected]", "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/"
            + "[email protected]" + "/thin";
    System.err.println("This is the url:" + url);
    try {
        feedUrl = new URL(url);
        service = new ContactsService("Google-contactsExampleApp-3");
        service.setUserCredentials("[email protected]", "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

    }


}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-01-01 21:54:11

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

<uses-permission android:name="android.permission.INTERNET" />

Are you sure you're allowed to use the network connection? Make sure this permission is in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
假扮的天使 2025-01-01 21:54:11
 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
 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文