java - 使用 Ajax 调用将 Cordova 9 移动应用程序连接到 Java Web 服务

标签 java android jquery ajax cordova

我对 Cordova 非常陌生,我已经安装了 Cordova 版本 9,我正在 android studio 上开发它。 我对用 Java 编写的 Web 服务层进行了简单的 ajax 调用。由于某种原因,我无法从我的移动应用程序连接到我的网络服务,它返回 404 状态。相同的 URL 是从模拟器的浏览器连接的,但不是从开发的应用程序连接的。我已在 config.xml 中将我的网址列入白名单。下面是config.xml和ajax代码。

配置.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloWorld</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <preferance name="android-usesCleartextTraffic" value="true" />
    <access origin="*" subdomains="true"/>
    <access origin="http://192.168.0.111:8091" subdomains="true"/>
    <allow-navigation href="http://*/*" />
    <allow-navigation href="https://*/*" />
    <allow-navigation href="data:*" />
    <allow-navigation href="http://192.168.0.111:8091/*" />
    <allow-navigation href="*" />

    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="http://192.168.0.111:8091/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">        
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

Ajax 代码:

document.getElementById("btnLogin").addEventListener("click", onClickOfLogin);
    function onClickOfLogin(){
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            url: "http://192.168.0.111:8091/ZeOMSWS/AppLogin",
            crossDomain: true,
            success: function(resultObj) {
                navigator.notification.alert(
                    'Registration request has been sent for HR approval. You will receive an email once approved and then you can proceed with Login.', 
                    onSuccessDismissOfAlert, 
                    'Info', 
                    'OK');
            },
            error: function(request, status, error) {
                console.log("Error status " + status);
                console.log("Error request status text: " + request.statusText);
                console.log("Error request status: " + request.status);
                console.log("Error request response text: " + request.responseText);
                console.log("Error response header: " + request.getAllResponseHeaders());
                navigator.notification.alert(
                    'Error Occured.' + error, 
                    onErrorDismissOfAlert, 
                    'Info', 
                    'OK');
            }
        });
    }

正如我之前提到的,我使用的是带有 SDK 版本 29 的 Cordova 9,模拟器位于 Android 10 上。 我已经被这个问题困扰了两天多了。我在谷歌上尝试了很多解决方案,但我不明白我哪里出了问题。请帮助我知道我缺少什么。

任何帮助将不胜感激。

谢谢

最佳答案

问题是在 Android SDK 版本 29 中默认允许 ajax 调用 https 服务。

无法发送 HTTP 请求。

只需将应用和服务器响应中的 URL 从 http://更改为 https://

否则:

  1. 在 config.xml 中添加小部件:
    <widget xmlns:android="http://schemas.android.com/apk/res/android">
  • config.xml中编辑平台android:
  •     <platform name="android">
            <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
                <application android:usesCleartextTraffic="true" />
            </edit-config>
        </platform>
    

    希望有用

    Protecting users with TLS by default in Android P 1

    Android高版本http网络请求失败的Cordova配置处理

    关于java - 使用 Ajax 调用将 Cordova 9 移动应用程序连接到 Java Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436022/

    相关文章:

    javascript - 如何在 js.erb 中使用 Ruby 代码?

    java - 具有相同名称字段的 DbUtils QueryRunner

    java - Spigot 服务器未写入配置文件

    java - 处理主 Activity 中的外部类对话框按钮

    android - 使用 htaccess 在 https 页面上使用 android 发布请求

    javascript - Jquery 和 Javascript 中数组的区别

    jquery - Rails4 - 如何动态设置div 的背景图像?

    java - 强制多个线程在可用时使用多个 CPU

    android - Android Studio Gradle在加载旧的gradle项目期间挂起

    android - android中的异步任务可以自动停止他们的任务吗?