1

Im having a problem with a XMLHttpRequest on Android using Eclipse....

If I build and run the app on a connected device, I get no error but when I build the apk and then install on the device I get an error so Im missing a permission or something. Any help appreciated.

      var send = function() {
            a = new XMLHttpRequest();
            a.open((type.toLowerCase()==="post")?"POST":"GET",self._prepareUrl(url),true);//+'&username='+data.userid,true);
            a.onreadystatechange = rsc;
            a.timeout = 50000;
            setTimeout(function () {     /* vs. a.timeout */
                if (a.readyState < 4) {
                    a.abort();
                }
            }, 50000);        

            a.ontimeout = to;
            a.onerror = err;
            a.onabort = errAbort;

            a.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
            a.send(resData);
        };

The error function is:

    err = function(){
      alert('send fail: stat: ' +a.status +' state: ' +a.readyState +' rtext: ' +a.responseText);
      },

and the message I get is:

status = 0 state = 4 no response text

so the request has been aborted but why? I must be missing something in the below files as it does work when I run the app via eclipse which must allow something by default but when the apk is built and installed on the device I get the allow permissions info screen on install which does not appear when running via eclipse.

the manifest is:

<?xml version='1.0' encoding='utf-8'?>
<manifest 
android:versionCode="5" 
android:versionName="1.3" 
package="XXXXX" 
android:windowSoftInputMode="adjustPan"
xmlns:android="http://schemas.android.com/apk/res/android">

<supports-screens 
    android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:resizeable="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true"
/>

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name" 
    android:hardwareAccelerated="true">
    <activity  android:name="CordovaApp" android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

and the cofig.xml is:

<?xml version='1.0' encoding='utf-8'?>
<widget id="XXXXXXX" version="0.0.1" xmlns="http://www.w3.org/ns/widgets"     
xmlns:cdv="http://cordova.apache.org/ns/1.0">

<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<access origin="*" subdomains="true" />

<log level="DEBUG"/>
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="true" />
<preference name="disallowOverscroll" value="true" />
<preference name="loadUrlTimeoutValue" value="60000" />

<feature name="Camera">
    <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<feature name="File">
    <param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="Device">
    <param name="android-package" value="org.apache.cordova.device" />
</feature>
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.network-information" />
</feature>
<feature name="SplashScreen">
    <param name="android-package" value="org.apache.cordova.splashscreen" />
</feature>
<feature name="Globalization">
    <param name="android-package" value="org.apache.cordova.globalization" />
</feature>

Franko73
  • 13
  • 1
  • 4

2 Answers2

0

After a bit of searching I found the answer.....

The Android WebView does not allow by default self-signed SSL certs. When app is debug-signed the SSL error is ignored, but if app is release-signed connection to untrusted services SSL errors are not ignored.

Phonegap Android app ajax requests to HTTPS fail with status 0

Community
  • 1
  • 1
Franko73
  • 13
  • 1
  • 4
0

If Intel XDK as an alternative developer platform is an option for you, the XMLHTTPRequest will work.

Please see my code here: XMLHTTPrequest and CrossWalk Android ( edit ..after i found a solution !) generating the APK as a HYBRID Legacy APP!

I also have a problem with this but it is related to crosswalk. But just the Android APK without crosswalk is running fine. I fear that you run in this same origin policy issue. Maybe you should double-check this on you side.

My application does not have a specific manifest. XDK is managing and compiling this. This part is still a blackbox for me, as I am quite new in this stuff...

This code is working fine on my side

//***************************************************************************************************
// WS  THIS IS THE WS SECTION
//***************************************************************************************************

function XHRObject() {
    var xhr;

    xhr = new XMLHttpRequest();

    xhr.onerror = function () {};
    xhr.onstart = function () {};
    xhr.success = function () {};

    return xhr;
}

function getSphereParametersWSxhr() {
    var url = "http://192.168.2.171/transporter.asmx/getVideoCubeParameters";
    xhr = new XMLHttpRequest();
    var params = "";

    console.log(xhr);
    alert("before open POST : " + xhr);
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // forbidden xhr.setRequestHeader("Content-length", params.length);
    alert("after open POST : " + xhr);

    try {
        xhr.onreadystatechange = function () {

            alert ("xhr.readyState == " + xhr.readyState + " xhr.status == " + xhr.status + " xhr.statusText: " + xhr.statusText + " xhr.responseText" + xhr.responseText );

            if (xhr.readyState == 2 && xhr.status == 404) {
                console.log("404 page not found: " + xhr);
                alert("404 page not found: " + xhr);
            }
            if (xhr.readyState == 3) {
                console.log("ready state 3: " + xhr.statusText + " " + xhr.status);
                alert("ready state 3: " + xhr.statusText + " " + xhr.status);
            }
            if (xhr.readyState == 4 ) {                                                         //&& xhr.status == 200
                console.log("ready state 4: " + xhr.statusText + " " + xhr.responseText);
                alert("ready state 4: " + xhr.statusText + " " + xhr.responseText);

                var erg1 = xhr.responseXML.getElementsByTagName("spherePictures")[0].textContent;
                var stringList = erg1.split(";");
                console.log(erg1);
                alert("pictureList: " + erg1);
                alert(xhr.responseText);

            }

        }
        xhr.send(params);
    } catch (e) {
        console.log("XHR Post : " + e);
        alert("XHR Post : " + e);
    }
}
Community
  • 1
  • 1
kina
  • 41
  • 6