Ionic run android works BUT apk does not access internet


#1

When I run ionic run android, my app successfully calls REST api’s. However, when I create signed apk and install (goes fine) onto my device the app cannot successfully call REST api. I should add…I download/install the apk from my enterprise and NOT the Play Store. Also, I’ve tried it with cordova command, as well as standard “ant release” command.
Note --> I have access to server and enabled CORS. Running in browser also works (using python -m SimpleHTTPServer 8000). I’m guessing an android “thing”.


#2

Hi,
Did you set:

<access origin="*" />

in your config.xml?

You may also verify if Cordova CLI has generated the Android permission entries in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

#3

origin is already set to “*”. I did not have ACCESS_NETWORK_STATE set. However, this did not help.


#4

Well if “access origin” is set to ‘*’ and Android permissions are fine it is maybe something with the HTTP request.
It is also obvious but do you set the Access-Control-Allow-Origin header in your HTTP requests? Something like this:

$http.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

By the way, did you check the network connection on your Android device?? :smile:


#7

Just to close the loop…it was an android issue with my intermediate certs. Evidently, when using the debug.apk (ionic run android) android must not follow “rigid” pki certificate chaining. However, when using released app (with signed/zipaligned apk) it was checking intermediate certs. I updated my https proxy with appropriate intermediate certs (they were missing) and it worked.


#8

Hi kkraus,

I’m having the same issue when running ionic as an android app on my phone. The app is able to retrieve data via Parse.com when the phone is on wifi. It stops working when wifi is turned off. I tried what you suggested using released app (with signed/zipaligned apk), it’s still not working. What am I missing? Thanks.


#9

Hi kkraus,

I’m having a similar problem. My question is, how did you figure out it was the intermediate certs? What did you do to debug it?


#10

Any news? I’m having the same problem, works fine with Wifi.


#11

Worked over http. Did not work over https. So, I figured it was something with certs (ordering) on the server side or needing a client cert on the android. I had access to server-side “front-end” and knew it was not asking for client cert…so I figured i mucked something up w/ creating certs from GoDaddy on HaProxy. Turns out that’s what it was. I did not have to mess with client-side certs in ionic/android.
I found this blog post today…seemed pretty thorough and has code in case you need client-side modifications.
http://chariotsolutions.com/blog/post/https-with-client-certificates-on/


#12

I have the same problem with accessing a rest http service. I do not use https and have ‘’ in my config.xml. This problem only occurs when i work with the signed, release apk.
Any ideas?

Update: Im sorry somehow my plugins got deleted. so that was pretty much the problem :smile:


Ionic mobile app $http fails to connect. But in browser it works fine
#13

w/o understanding all ur steps, have you made sure it’s not a CORS thing? Does your unsigned apk work locally (same domain), as well as to remote (different domain) rest API?


#14

I was missing the whitelist plugin:

ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git

After adding everything worked as aspected


Ionic mobile app $http fails to connect. But in browser it works fine
#15

I have a similar problem, the browser works perfectly but when installing the device or emulate simply does not perform the request, checked my files and my AndroidManifest.xml config.xml file and the information below respectively list the permissions:

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

and config.xml

<access origin="*"/>

someone help me know I’m desperate to deliver the project and this is delaying me more than usual.


#16

If you updated recently (running cordova 5.0), my guess would be the cordova-plugin-whitelist needs to be installed. After installing, add this to config.xml to allow all http/s requests :
allow-intent href="http:///"
allow-intent href=“https:///


#17

That was the problem install the plugin and it worked perfectly! :smile:


#18

Same for me, thanks a lot!


#19

Not work for me…
add this to config.xml :

   <access origin="*"/>                    
   <allow-intent href="*" />
   <allow-navigation href="*" />

Any ideas ? Please help!
Not work only release build, debug build work fine.
Whitelist plugin and 5.0.0 cordova installed.


Cordova/Ionic CLI hooks for modifying AndroidManifest.xml or IOS PList file?
#20

Next guess (since I don’t know ur architecture) is to look at server-side and “CORS”. If ur running node with express backend, here’s a sample whitelist/cors “configuration”:
var whitelist = [‘http://localhost:8100’, ‘http://localhost:9000’, ‘http://localhost:8080’];
var corsOptions = {
origin: function(origin, callback){
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
}
};
app.use(cors(corsOptions));


#21

Wow, thanks for fast reply, may i send you example with jquery requests to my api ? where can i send it (i think i just drop it here)?
i dont have acces to server side its works on java jwt and they add all headers needed for cors, and its cant be cors problem becaues debug version work well on device, only after cordova build --release android its stop working…
Device Lenovo P780, android version 4.4

Ther is my test www folder: https://drive.google.com/open?id=0B869Sv33xwGWNWdaUkRIY0dPSTg&authuser=0


#22

Ok like kkraus said its “Worked over http. Did not work over https” so how fix that? how set certifcate ?