无法在 Android 应用程序中获取 Gmail 联系人 - 可以使用相同的代码从桌面 Java 应用程序获取
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定您可以使用网络连接吗?确保此权限位于您的
AndroidManifest.xml
文件中:Are you sure you're allowed to use the network connection? Make sure this permission is in your
AndroidManifest.xml
file: