I am trying to connect to server for fetching data using ajax call inside phonegap app.
But I am getting status 404 for every request I tried google.com, 10.0.2.2 (localhost on android).
Here are steps I followed for creating application
I created template application using phonegap command line as phonegap create my-app
Running application using phonegap run android ( This shows "device is ready" screen on emulator )
Added following code for making an Ajax call to index.js - receivedEvent function
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
console.log('rdy state - '+ xmlhttp.readyState);
console.log('status - '+ xmlhttp.status);
console.log('text status - '+ xmlhttp.statusText);
console.log('Response Text - '+ xmlhttp.responseText);
}
xmlhttp.open("GET","http://10.0.2.2/", true);
xmlhttp.send();
I can access http://10.0.2.2 from emulator browser.
Here is Enviornment I am using
Phonegap version - 3.4.0-0.19.8
Android version - 4.4.2
-- Vishal
Did you put an access token in your config.xml? Try to put
<access origin="*"/>
into your config.xml.
* gives access to all domain.
Look up http://docs.phonegap.com/en/3.4.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide for further information.
Despite you have:
<pre> <access origin="*"/> </pre> in config.xml
You need to add the whitelist to your phonegap(cordova) project:
phonegap plugin add cordova-plugin-whitelist
After this your app will no longer give 404.
Related
I'm building an ionic application and I faced with an issue that affect only the Android devices.
I have some public images that I'd like to load like this:
<img src={imageurl} alt="" />
I'm using this plugin https://github.com/capacitor-community/http to make API calls, to avoid CORS issues. All the API calls are working on both platforms, but the images only works on iOS.
If I check the inspector this is the error message I get:
If I check the Android Studio logs I see these error messages: E/Capacitor: Unable to open asset URL:
If I'm using axios to make the API calls it works fine on Android but then it'll break on iOS due to CORS issues.
I was trying to separate the API calls and only use the native plugin on iOS but I don't think that is possible.. Android was able to make the calls with axios but response was always got response like this:
Android AndroidManifest is extended with this: android:usesCleartextTraffic="true"
webDir: "build",
bundledWebRuntime: false,
android: {
allowMixedContent: true
},
server: {
cleartext: true,
hostname: "baseurl.hu",
iosScheme: "localhost",
androidScheme: "https"
},
This is how I run the app:
ionic capacitor copy android
Android Studio run.
I try to use Angularjs http service to call my PHP API. Everything working fine in Browser. But, when I test it on my Android Phone, it will not work.
My Issue:
In Android PhoneGap Application, Whenever I trigger the Angularjs http service it will always trigger the function (error) even when the Mobile is connected with Internet.
I have try look this but their solution aren't working:
Angular $http.get() does not work in Phonegap DevApp
Cordova 5.3.1 Android app can't access the internet
I also have ensure the Phonegap config.xml has allowed Internet Permission Access with:
<access allows-arbitrary-loads-in-media="true" allows-arbitrary-loads-in-web-content="true" allows-local-networking="true" origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />
However it is still not Working.
My Question:
Is Angularjs http service is not able to work on PhoneGap Mobile Application but only Web Browser?
Aside from config.xml , is there any other configuration have to be made to allow PhoneGap Mobile Application to access the Internet?
Angularjs code:
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$http({
method: 'POST',
data: {
'email' : $scope.email,
'password' : $scope.password,
},
url: 'https://unsurfaced-cross.000webhostapp.com/PGLogin.php'
}).then(function (response){
$scope.users = response.data;
if(response.data[0]=="LOGIN"){
alert("Successful Login");
$window.location.href = './homepage.html';
}else{
alert("Login Failed. Incorrect Email or Password.");
}
},function (error){
alert("Please ensure You are connected to Internet.");
});
seem like actually I just didn't install the whitelist plugin, for my case the whitelist plugin didn't installed by Default.
Since I was using PhoneGap, instead of using command like cordova-plugin-whitelist#latest, cordova create, cordova plugin add cordova-plugin-whitelist or etc.
I should use: phonegap plugin add cordova-plugin-whitelist
After install the Whitelist Plugin, everything working fine.
So,
Angularjs http service is able to work on PhoneGap Mobile Application.
Aside from config.xml , we have to ensure that all necessary and required plugin was installed such as Whitelist plugin.
I have just upgraded from Cordova v3.4 to latest 6.1.1. My whole android app is working just fine apart from ALL jquery Ajax calls here is a sample one:
var baseUrl = 'http://gazetkisklepowe.pl';
$.ajax({
url: baseUrl+"/api/get/favourites",
data: {
user_uuid: device.uuid
}
}).done(function(data) {
}).fail(function() {
alert('Error connecting. (100) '+baseUrl+"/api/get/favourites");
return null;
});
I have read this article: PhoneGap / Android WebView throws "Unknown chromium error: 0" but I haven't found a solution for me.
The message in the LogCat just displays: "unknown chromium error: 0"
My config.xml contains:
<access origin="*" />
<access origin="mailto:*" launch-external="yes"/>
SN: I would like just to inform that previously on Cordova v3.4 all Ajax requests worked just fine and there have not been any changes in the code since then.
UPDATE 10am 5/04/2016
I have added whitelist plugin and <allow-navigation href="data:*" /> to the config.xml, now I get:
object has no method 'error' at file ///android_asset/www/plugins/cordova-plugin-whitelist/whitelist.js:24
Line 24 in that js file is: console.error(msg); so from some reason console is not fully defined.
UPDATE 2pm 05/04/2016
I have created new cordova app in separate folder and copied my files over, that done the trick and have fixed everything.
I am trying to force the links in my app to open in external browser. I am working with jQuery mobile & Phonegap build.
My app has a page with generated contact details. Among the details there is an link to the person's website:
row+='<div class="accWWW"> '+ data[i].website +'</div>';
I have these 2 functions defined in my code.
//function which forces open in browser
function openBrowser(url){
if(device.platform === 'Android') {
navigator.app.loadUrl(url, {openExternal:true});
} else {
window.open(url, '_system');
}
}
//opens generated URL's
function openGenerated(obj){
var url = obj.getAttribute("href");
console.log(url);
openBrowser(url);
return false;
}
I also have a simple <div class="accWWW"> Google </div> set in my index.html
So... this link to Google opens perfectly in iOS (haven't yet tested on Android because iOS was my biggest problem), but the generated link wouldn't do anything if I hadn't added the openGenerated function. Now there is an action, but it opens inside the app.
Do you have any idea why that is?
Also when checking on the laptop I get these errors in the console( link is my development url):
Uncaught ReferenceError: device is not defined *link*:277 // makes sense probably, because I am not using a mobile device
Uncaught SecurityError: Blocked a frame with origin "http://www.test.com" from accessing a frame with origin "*link*". Protocols, domains, and ports must match.// what is this?
I was thinking maybe it's an access issue, but I added <access origin="*" browserOnly="true" /> in my config.xml, so there shouldn't be a problem with this.
LATER EDIT: On Android the Google link doesn't work at all and the generated one opens inside the app... Apparently device.platform is always null, but the device plugin is added in xml as I can see in phonegap build this list of plugins:
Installed Plugins
Apps will be built with the latest version of a plugin unless you lock the version in your config.xml (see below). -- Plugin installed as a dependency of another plugin
Third Party VERSION LATEST VERSION
com.phonegap.plugin.statusbar 1.1.0 1.1.0
PhoneGap Core
org.apache.cordova.device 0.2.8 0.2.8
org.apache.cordova.inappbrowser 0.3.3 0.3.3
LATER EDIT:
Device.platform doesn't seem to be working, so I modified my function, adding Ved's suggestion, to:
function openBrowser(url){
if(navigator.app)
navigator.app.loadUrl(url, {openExternal:true});
else {
var ref = window.open(url, '_system', 'location=no');
ref.addEventListener('loadstart', function(event) {
});
ref.addEventListener('loadstop', function(event) {
console.log(event.type + ' - ' + event.url);
});
ref.addEventListener('exit', function(event) {
});
}
}
on iOS still opens inside the app. For Android I think it's working fine.
I fixed my issue.
I was adding cordova.js in my html. Apparently this broke things.
i have a working PhoneGap/Cordova Project Ver 3.5.
i try to work with GA with this plugin, But without success:
https://github.com/danwilson/google-analytics-plugin
i success to install the plugin via this command:
cordova plugin add https://github.com/danwilson/google-analytics-plugin.git
when i try the usage the plugin it faild.
for example:
analytics.startTrackerWithId('UA-XXXX-YY') // UX-2323-23 for example
Any "analytics" function that i try to use fail with error:
"analytics is not defind"
i try via local server - get "analytics is not defind"
i try in simulator - get empty and white screen
i try on Real Device and also get empty and white screen
Please Help :)
Thanks,
Chen.R
First, provide access to external URL using in config.xml <access origin="*" /> then
Put this analytics.startTrackerWithId('UA-XXXX-YY') into onDeviceReady function.
And then you can write your own page names like analytics.trackView('page name');
When you are on the google analytics account, please check the date and time set for view report on right side corner.