简体   繁体   English

无法从Cordova Android应用程序调用本地Web API

[英]Cannot call local web api from cordova android app

I am currently developing a cordova 4.3 android app in Visual Studio 2015 RC. 我目前正在Visual Studio 2015 RC中开发cordova 4.3 android应用程序。 It's an AngularJS app and I use $resource to send requests to an ASP.NET Web Api application running on a different port on the same computer. 这是一个AngularJS应用程序,我使用$ resource将请求发送到在同一台计算机上不同端口上运行的ASP.NET Web Api应用程序。 Debugging the app in Ripple works fine if I disable the Cross Domain Proxy but when I try to debug in the Visual Studio Android Emulator or on my Android device I get a not very helpful "Failed to load resource" as soon as try to reach the web api. 如果我禁用跨域代理,则在Ripple中调试应用程序可以很好地工作,但是当我尝试在Visual Studio Android仿真器或Android设备中进行调试时,尝试达到该目标后,会收到一个非常有用的“无法加载资源”信息网络API。 The url looks correct and I suspect the problem has to do with either CORS or my HTTPS certificate or both? 网址看起来正确,我怀疑问题与CORS或我的HTTPS证书或两者有关? What I have tried so far is: 到目前为止,我尝试过的是:

  1. Set <uses-permission android:name="android.permission.INTERNET" /> and android:debuggable="true" in the manifest file. 在清单文件中设置<uses-permission android:name="android.permission.INTERNET" />android:debuggable="true"
  2. Tried chrome://inspect/#devices to get more info about the problem but got the error message "Cannot load DevTools frontend from an untrusted origin". 尝试chrome:// inspect /#devices以获取有关该问题的更多信息,但收到错误消息“无法从不受信任的来源加载DevTools前端”。
  3. I am able to connect my android phone chrome web browser to the localhost web api using the very helpful tool sharpproxy [ https://www.codefromjames.com/wordpress/?p=97] but this approach unfortunately does not work with the app's webview. 我可以使用非常有用的工具Sharpproxy [ https://www.codefromjames.com/wordpress/?p=97]将我的android手机chrome网络浏览器连接到localhost网络api,但不幸的是,这种方法不适用于该应用程序的网页流量。
  4. The ajax request does work when I point the app to my azure hosted live web api endpoint where I have a valid https certificate but that is not very convenient to use while developing the app. 当我将应用程序指向我的azure托管实时Web api端点时,ajax请求确实起作用,在该端点上我具有有效的https证书,但是在开发应用程序时使用起来不太方便。
  5. Made sure the config.xml-file contains <access origin="*" /> 确保config.xml文件包含<access origin="*" />
  6. My web api is configured with app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll) 我的Web API使用app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll)配置

I am completely new to cordova/hybrid app development and am surely missing some obvious thing? 我对Cordova /混合应用程序开发完全陌生,并且肯定缺少一些明显的东西吗? Any help or suggestion would be very appreciated!! 任何帮助或建议将不胜感激!

After having all sort of problems with Visual Studio 2015 I uninstalled it went back to Visual Studio 2013 Update 4 and now I can connect my android device to a local running web api using sharp proxy I mentioned and linked to above. 在遇到Visual Studio 2015的各种问题后,我将其卸载到Visual Studio 2013 Update 4,现在我可以使用上面提到并链接到的夏普代理将android设备连接到本地运行的Web api。

So to be clear: 因此要明确:

  1. Use Visual Studio 2013 Update 4 to run your web api on for example the url https://localhost:44358/ . 使用Visual Studio 2013 Update 4在例如URL https:// localhost:44358 /上运行Web api。
  2. Start sharp proxy with external port 5000 and internal 44358. 使用外部端口5000和内部44358启动Sharp代理。
  3. Make sure your android device is connected to the same wifi and instruct your app to use https://your-ip-address:5000 for all service calls. 确保您的android设备已连接到同一wifi,并指示您的应用对所有服务呼叫使用https://您的IP地址:5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM