Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-23309] Android: Redirects are not being followed in HTTPClient #8043

Merged
merged 5 commits into from Jul 11, 2016

Conversation

ashcoding
Copy link
Contributor

JIRA: https://jira.appcelerator.org/browse/TIMOB-23309

See ticket for test case.

@ashcoding ashcoding closed this Jun 6, 2016
@ashcoding ashcoding reopened this Jun 7, 2016
@ashcoding
Copy link
Contributor Author

Classic test code:-

var OS_ANDROID = true;

function resourcePDF(e) {
    openResourcePDF();
}

function urlPDF(e) {
    openURLPDF();
}

function viewPDF(appfilepath) {
    Ti.API.info("A:"+appfilepath);
    if (OS_ANDROID) {
        try {
            Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
                action : Ti.Android.ACTION_VIEW,
                type : 'application/pdf',
                data : appfilepath
            }));
        } catch(e) {
            Ti.API.info('error trying to launch activity, e = ' + e);
            alert('No PDF apps installed!');
        }
    } else {
        docViewer = Ti.UI.iOS.createDocumentViewer({
            url : appfilepath
        });
        docViewer.show();
    }
}

function openResourcePDF() {
    var appFile;

    if (OS_ANDROID) {
        //copy from app directory to SDCard (once)
        var originalFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'pdf/sample.pdf');
        appFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'sample.pdf');
        if (appFile.exists() === false) {
            appFile.write(originalFile.read());
        }
    } else {
        appFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'pdf/sample.pdf');
    }
    var appfilepath = appFile.nativePath;

    viewPDF(appfilepath);
}

function openURLPDF() {
    var appFile;

    if (OS_ANDROID) {
        appFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'map.pdf');
    } else {
        appFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map.pdf');
    }
    var appfilepath = appFile.nativePath;

    //Check if file has been downloaded yet
    if (appFile.exists() === false) {
        var xhr = Ti.Network.createHTTPClient();
        xhr.onload = function() {
            Ti.API.info('status code ' + this.status);
            Ti.API.info(this.responseData);
            appFile.write(this.responseData);
            viewPDF(appfilepath);

        };
        xhr.onerror = function() {
            alert("Cannot retrieve PDF form web site");
        };
        xhr.timeout = 10000;
        xhr.open("GET", "http://dev-cms-molpower.trafficmanager.net/ServiceProfileFileHandler.ashx?svc=CMI&file=FirstSchedule.pdf");
        xhr.send();
    } else {
        viewPDF(appfilepath);
    }


}


var win = Ti.UI.createWindow({
    backgroundColor : 'black',
    layout : 'vertical'
});

var button1 = Titanium.UI.createButton({
title: 'resourcePDF',
top: 30,
width: 100,
height: 50
});

var button2 = Titanium.UI.createButton({
title: 'urlPDF',
top: 100,
width: 100,
height: 50
});


button1.addEventListener('click',function(e)
{
    Ti.API.info("Pressed");
    resourcePDF();

});
win.add(button1);

button2.addEventListener('click',function(e)
{
    Ti.API.info("Pressed");
    urlPDF();
});
win.add(button2);

win.open();

@ashcoding
Copy link
Contributor Author

Do note that you should have an app on Android that can open a PDF file for it to successfully launch and open the PDF.

…or normal situations where it is from HTTP to HTTP or HTTPS to HTTPS.
@ashcoding
Copy link
Contributor Author

ashcoding commented Jun 30, 2016

Had to merge Master due to conflict.

This was originally merged from
tidev#8003 and added by
m1ga. Due to merge conflicts, this code was moved and this commit is to
put it back in place.
@ashcoding
Copy link
Contributor Author

Merging

@ashcoding ashcoding merged commit 8d8d192 into tidev:master Jul 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants