Http请求仅在释放Android APK时失败 [英] Http requests fails only while releasing Android APK

查看:139
本文介绍了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

推荐答案

我前一段时间也遇到了同样的问题.就我而言,问题是证书无效.在iOS上以及在Android上进行测试时,该证书在浏览器中看起来都很不错(chrome甚至显示了绿色锁定).但是我让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-cordova-android-ios/

处理此问题的最佳方法是将证书固定在您的应用中.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆