无法获取在Android应用程序的Gmail联系人 - 能从桌面的Java应用程序与同code取 [英] Unable to fetch Gmail Contacts in Android app - Can fetch from Desktop Java App with same code

查看:95
本文介绍了无法获取在Android应用程序的Gmail联系人 - 能从桌面的Java应用程序与同code取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是两个应用程序的code。我试图在仿真器和手机Android应用程序。在这两个,我得到了同样的错误。我可以打开两个网页浏览器页面(我的意思是,我有互联网接入)。请指教。

  // Java应用程序code
公共静态无效的主要(字串[] args)抛出ServiceException,IOException异常{
    ContactsService服务;
    URL feedUrl;
    ContactFeed resultFeed;
    字符串URL =htt​​ps://www.google.com/m8/feeds/+的联系人/
            +username@gmail.com+/瘦;
    通信System.err.println(这是网址:+网址);
    尝试{
        feedUrl =新的URL(网址);
        服务=新ContactsService(谷歌-contactsExampleApp-3);
        service.setUserCredentials(username@gmail.com,密码);
        resultFeed = service.getFeed(feedUrl,ContactFeed.class);
        为(ContactEntry条目:resultFeed.getEntries()){            如果(entry.getTitle()!= NULL){                通信System.err.println(联系人姓名:
                        + entry.getTitle()getPlainText())。            }其他{
                通信System.err.println(联系人没有名字);            }
        }
    }赶上(例外五){
        通信System.err.println(异常:+ e.getMessage());
    }
}// Android应用程序code
公共无效的onClick(查看为arg0){    ContactsService服务;
    URL feedUrl;
    ContactFeed resultFeed;    字符串URL =htt​​ps://www.google.com/m8/feeds/+的联系人/
            +username@gmail.com+/瘦;
    通信System.err.println(这是网址:+网址);
    尝试{
        feedUrl =新的URL(网址);
        服务=新ContactsService(谷歌-contactsExampleApp-3);
        service.setUserCredentials(username@gmail.com,密码);
        resultFeed = service.getFeed(feedUrl,ContactFeed.class);
        为(ContactEntry条目:resultFeed.getEntries()){            如果(entry.getTitle()!= NULL){
                Toast.makeText(getApplicationContext(),
                        联系人姓名:+ entry.getTitle()getPlainText()
                        Toast.LENGTH_SHORT).show();            }其他{
                Toast.makeText(getApplicationContext(),
                        跟没有名字,Toast.LENGTH_SHORT).show();            }        }
    }赶上(例外五){
        Toast.makeText(getApplicationContext(),
                例外:+ e.getMessage(),Toast.LENGTH_SHORT).show();
        的System.out.println(****异常:+ e.getMessage());        //异常:错误登录URI连接    }
}
}


解决方案

 在Android模拟器,你会发现电子邮件application.You只需点击并给予电子邮件ID,然后password.Login中account.then在模拟器运行code它将作品

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/"
            + "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

    }


}
}

解决方案

 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

这篇关于无法获取在Android应用程序的Gmail联系人 - 能从桌面的Java应用程序与同code取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆