android HTTPClient POST problems

You must Login before you can answer or comment on any questions.

I have the following code and anytime it's executed createReq.onError is called. What's wrong here? Using latest mobile SDK and android emulator. I'm sure that "somehost" (removed actual host) is working!

var createReq = Titanium.Network.createHTTPClient();  
createReq.open("POST","http://somehost/post_register.php");  
                var params = {  
                    username: username.value,  
                    password: Ti.Utils.md5HexDigest(password1.value),  
                    names: names.value,  
                    email: email.value  
                };  
                createReq.send(params);

3 Answers

Accepted Answer

i have had an incredible amount of problems with the httpClient on android and was finally forced to use the 1.6 CI builds to get it to work.

I would suggest you try the latest CI build an see of your problem is solved and then take a look in lighthouse to get additional information

lighthouse == titanium mobile bug list

In your onError callback, log or show an alert with the values of createReq.status and createReq.statusText which should let you decide what the error is. The status is normally 200 but you are likely getting either a 4xx or 5xx error. The combination of the error code and text should let you debug it.

You're not giving us enough information to decipher what your problem is -- it may even just be an authentication error.

— answered 1 year ago by Doug Handy
answer permalink
2 Comments
  • > status=0, statusText=null is this even possible?

    — commented 1 year ago by Daniel Ferenc

  • I have seen that on occasion with android myself with a specific server, even with the 1.6.x builds. However I need to run the 1.6.x builds anyway due to other problems they address. I've tended to dismiss it as a server problem, because I have only witnessed it with one server. Perhaps it is not the server?

    — commented 1 year ago by Doug Handy

hello i'm obtein tha same result status=0, statusText=null my code is: var win = Titanium.UI.currentWindow;

/* * Interface */ var scrollView = Titanium.UI.createScrollView({ contentWidth:'auto', contentHeight:'auto', top:0, showVerticalScrollIndicator:true, showHorizontalScrollIndicator:true }); win.add(scrollView);

var username = Titanium.UI.createTextField({ color:'#336699', top:10, left:10, width:300, height:40, hintText:'Username', borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(username);

var password1 = Titanium.UI.createTextField({ color:'#336699', top:60, left:10, width:300, height:40, hintText:'Password', passwordMask:true, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(password1);

var password2 = Titanium.UI.createTextField({ color:'#336699', top:110, left:10, width:300, height:40, hintText:'Password Again', passwordMask:true, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(password2);

var names = Titanium.UI.createTextField({ color:'#336699', top:160, left:10, width:300, height:40, hintText:'Name', borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(names);

var email = Titanium.UI.createTextField({ color:'#336699', top:210, left:10, width:300, height:40, hintText:'email', borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(email);

var country = Titanium.UI.createTextField({ color:'#336699', top:260, left:10, width:300, height:40, hintText:'Country', borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(country);

var state = Titanium.UI.createTextField({ color:'#336699', top:310, left:10, width:300, height:40, hintText:'State', borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); scrollView.add(state);

var createBtn = Titanium.UI.createButton({ title:'Create Account', top:360, width:130, height:35, borderRadius:1, font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14} }); scrollView.add(createBtn);

var testresults;

function checkemail(emailAddress) { var str = emailAddress; var filter = /^([A-Za-z0-9_-.])+\@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/; if (filter.test(str)) { testresults = true; } else { testresults = false; } return (testresults); };

var createReq = Titanium.Network.createHTTPClient(); createReq.onload = function() { if (this.responseText == "Insert failed" || this.responseText == "That username or email already exists") { createBtn.enabled = true; createBtn.opacity = 1; alert(this.responseText); } else { var alertDialog = Titanium.UI.createAlertDialog({ title: 'Alert', message: this.responseText, buttonNames: ['OK'] }); alertDialog.show(); alertDialog.addEventListener('click',function(e) { win.tabGroup.setActiveTab(0); }); } };

createBtn.addEventListener('click',function(e) { var name=username.value ; if (username.value != '' && password1.value != '' && password2.value != '' && names.value != '' && email.value != '') { if (password1.value != password2.value) { alert("Your passwords do not match"); } else { if (!checkemail(email.value)) { alert("Please enter a valid email"); } else { createBtn.enabled = false; createBtn.opacity = 0.3; createReq.open("POST","http://localhost:80/titanium/post_register.php?name="+name);

            var params = {
                username: username.value,
                password: Ti.Utils.md5HexDigest(password1.value),
                names: names.value,
                email: email.value

            };
            createReq.setRequestHeader('Content-Type', 'text/html; charset=utf-8');
            createReq.send(params);
            alert(createReq.connected);
            alert(createReq.status);
            alert("message"+createReq.statusText);
        }
    }
}
else
{
    alert("All fields are required");
}

});

createReq.onload = function() { alert("onload"); if (this.responseText == "Insert failed" || this.responseText == "That username or email already exists") { //win.remove(overlay); alert(this.responseText); } else { var alertDialog = Titanium.UI.createAlertDialog({ title: 'Alert', message: this.responseText, buttonNames: ['OK'] }); alertDialog.show(); alertDialog.addEventListener('click',function(e) { win.tabGroup.setActiveTab(0); }); } };

Icant access to the databse.i'm locate post_register.php under the www folder if titanium.help me plz right now.

Your Answer

Think you can help? Login to answer this question!