使用 AngularJs 在 Android Cordova 应用程序中加载 Rest 数据

Loading Rest data in Android Cordova application using AngularJs

我有一个由 Cordova 创建的 Android 项目 使用本地主机上 spring Rest projet 提供的 REST 服务 使用 AngularJs,我尝试使用以下代码访问资源:

'use strict';
angular.module('myService', ['ngResource']).
    factory('Demande', function ($http, $resource) {

 $http.defaults.useXDomain = true;

 return $resource('http:// localhost:8080\:8080/springrestprojet/rest/myobject/:id', {}, {
 'save': {method:'PUT', 
 headers: {
 'Content-Type': 'application/json',
 'Access-Control-Allow-Origin':'*',
 'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
 'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept',
 'Access-Control-Request-Headers': 'X-Requested-With, accept, content-type',
 'Accept':'application/x-www-form-urlencoded, multipart/form-data',
 'Access-Control-Allow-Credentials': 'true'}},
 });
});

我可以打开文件

C:\Users\User\Mobile\AppClient\platforms\android\assets\www\index.html

这告诉我正确的 结果。

但是当我在模拟器中 运行 应用程序时,它只给我空视图(没有数据剩余)。 我将资源的 ip 地址更改为 10.0.2.2 和我机器的 ip 地址,但仍然无法正常工作!

我想我的模拟器 genymotion 设备和本地主机之间没有通信!

有什么建议吗?

谢谢。

Genymotion 在 VirtualBox 中 运行。您必须更改网络配置。如 http://bbowden.tumblr.com/post/58650831283/accessing-a-localhost-server-from-the-genymotion 所述:

The name of the network is vboxnet0, and if you run “ifconfig vboxnet0” (or “ipconfig vboxnet0” if running Windows) on your host machine, you should receive the IP address of your host on the vboxnet0 network. The default IP is most likely 192.168.56.1. This is the IP address to use when accessing your host machine from the Genymotion emulator.