本文介绍了Http 请求仅在发布 Android APK 时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使我的应用程序在 Android APK 版本上运行,它失败的唯一情况是生成和签署 apk.所有 http 请求都不起作用.(服务器在 SSL 下运行)

I'm struggling in making my app work on Android APK release, the only scenario it fails is at generating and signing the apk. All http requests doesn't work. (The server is running under SSL)

我已经尝试过的所有场景:

All Scenarios I've tried already:

  • ionic serve -> 工作正常.
  • ionic cordova run android --device -> 工作正常.
  • 也适用于模拟器.
  • ionic serve -> Works fine.
  • ionic cordova run android --device -> Works fine.
  • Works on emulators as well.

也可以很好地生成 iOS 版本:

Also works fine generating the iOS build:

  • ionic cordova build ios.
  • 在 Xcode 上,运行针对真实设备的构建.
  • 在 Xcode 上,将其存档并上传到 Itunesconnect,然后在 Apple 接受后从 AppStore 下载.

因此,唯一不起作用的情况是当我尝试通过 ionic cordova build android --prod --release 生成它的 apk 并对其进行签名时.

So, the only case it doesn't work is when I try to generate it's apk through ionic cordova build android --prod --release and signing it.

Google Play 也接受新的 APK,所以包标志完全没有问题.

Google Play also accepts the new APK, so there's no problem with the package sign at all.

由于它适用于 iOS 并直接在 Android 设备上运行,因此它不是 CORS 或 HTTPS 证书问题.

Since it works on iOS and running directly on android device, it isn't a CORS or HTTPS certificate problem.

代码:

login.ts 的片段:

snippet of login.ts:

this.userService.loginUser(this.user).then(
            (data) => {
                let response = data.json();
                loading.dismiss().then(loadData => {
                    if (response.access_token) {
                        this.global.access_token = response.access_token;
                        this.getUserData();
                    }
                });
            }, err => {
                let error = err.json();
                loading.dismiss().then(response => {
                    if (error.message) {
                        this.showToast(error.message, 3000, 'bottom');
                    }
                });
            }
        );

userService.loginUser 方法:

userService.loginUser method:

loginUser(data) {
    let headers = new Headers();
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('auth-token', '*');

    return this.abs.post('/authenticateMobile',
        {
            login_ds_email: data.email,
            login_ds_password: data.pass
        }, headers);
}

abs 在哪里:

constructor(http) {
    this.abs = new ApiService(http);
}

ApiService.post 方法:

ApiService.post method:

public post(api, params, header): any {
    if (!header) {
        header = this.getHeaders();
    }
    let options = new RequestOptions({headers: header});
    let url = this.global.urlGlobal + api;
    return this.http.post(url, params, options).toPromise();
}

当我触发登录"按钮并发出请求时,应用程序卡住了.服务器没有抛出异常,所以一直显示加载界面.

The App gets stuck when I fire "login" button and the request is made. There's no exception thrown by the server, so the loading screen is shown forever.

我已经没有办法解决这个问题了,希望你们能帮帮我.

I've running out of solutions for this and I hope you guys can help me out.

离子信息:

global packages:

@ionic/cli-utils : 1.4.0
Cordova CLI      : 6.4.0
Gulp CLI         : CLI version 3.9.1 Local version 3.9.1
Ionic CLI        : 3.4.0

local packages:

@ionic/app-scripts              : 1.3.0
@ionic/cli-plugin-cordova       : 1.4.0
@ionic/cli-plugin-gulp          : 1.0.1
@ionic/cli-plugin-ionic-angular : 1.3.1
Cordova Platforms               : android 6.0.0 ios 4.3.1
Ionic Framework                 : ionic-angular 3.0.1

System:

Node       : v7.8.0
OS         : macOS Sierra
Xcode      : Xcode 8.3.3 Build version 8E3004b
ios-deploy : 1.9.0
ios-sim    : 5.0.6
npm        : 5.0.3

推荐答案

我前段时间遇到了完全相同的问题.就我而言,问题是证书无效.该证书在浏览器(chrome 甚至显示绿色锁)、iOS 和 Android 上测试时对我来说都很好.但是我让安卓用户抱怨它对他们不起作用.

I had this exact same issue some time ago. In my case the problem was an invalid certificate. The certificate looked fine to me in the browser (chrome even showed the green lock), on iOS and while testing on Android. But I had android-users complain it wasn't working for them.

经过数小时的研究,结果发现对具有无效证书的网站的请求会被悄悄丢弃,这意味着没有任何反馈.没有错误,什么都没有.(如果我没记错的话,js 代码只是停止执行而不返回或执行任何回调.)这仅在以发布模式构建时发生.

After hours of researching it turned out that requests to websites with invalid certificates just get silently dropped, which means there is no feedback whatsoever. No errors, nothing. (If I remember correctly, the js code just stopped executing without returning or executing any callbacks.) This happens ONLY when building in release mode.

此处描述了一种解决方法:

A workaround is described here:

http://ivancevich.me/articles/ignoring-invalid-ssl-certificates-on-cordova-android-ios/

最好的处理方式是将证书固定在您的应用中.

The best way to handle it would be to pin the certificate in your app.

这篇关于Http 请求仅在发布 Android APK 时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:20