Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a json file read in Android using Ionic and AngularJS Frameworks?

I have a problem now regarding about the json that can't be read in my android..

the json file is where my data is place..it act as an static database..

I can get it with my Desktop but when it come to my mobile it didn't show..

Here is my sample code:

Here is my Services to get my json file..

var serviceUrl = '/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
    $scope.New = results;
});

Please help me to solve this problem.. my idea about the problem is the serviceUrl. Any idea about it. Thank you so much.. Im definitely a beginner for this Ionic Framework.

like image 510
Datz Me Avatar asked Oct 03 '14 10:10

Datz Me


2 Answers

Starting the serviceUrl with a '/' makes it an absolute URL. It will work in chrome since the root is the www folder. But in cordova mobile environment it will translate to file:///. Simply add a '.' ('./') to make it a relative path and it will work in both android and ios environments.

like image 155
meno Avatar answered Oct 08 '22 13:10

meno


To all who still in this problem I just find something that solve it. I don't know if it will solve in your problem but it really solve in me.. I use file:///android_asset/www/ as my serviceUrl

So this is an example:

var serviceUrl = 'file:///android_asset/www/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
    $scope.New = results;
});

Just try it and explore to it.. i just tried that one and it worked in me..

Maybe the reason is the all the json file in your apk installer will be placed in file:///android_asset/www/json directory in android phone so let your url point to that directory..

I hope it will help you and explore in it..that might not be the correct answer but i hope it will help you find the hint.

Thank you

like image 36
Datz Me Avatar answered Oct 08 '22 12:10

Datz Me