Warm tip: This article is reproduced from stackoverflow.com, please click
android cordova http ionic-framework apk

Http requests fails only while releasing Android APK

发布于 2020-04-08 23:40:24

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 -> Works fine.
  • ionic cordova run android --device -> Works fine.
  • Works on emulators as well.

Also works fine generating the iOS build:

  • ionic cordova build ios.
  • On Xcode, running build targeting a real device.
  • On Xcode, archiving and uploading it to Itunesconnect then downloading it from AppStore once it's accepted by Apple.

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 also accepts the new APK, so there's no problem with the package sign at all.

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

The code:

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 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);
}

Where abs is:

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

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.

Ionic info:

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
Questioner
Pedro Papadópolis
Viewed
188
Andreas Gassmann 2017-06-30 12:35

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.

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.