Search code examples
androidresttitaniumandroidhttpclient

Titanium : TiHttpClient error on host connecting


Description: I want to connect to server via POST request. My method, which posts the request:

function loginClick(e) {
    var url = "http://...";
    var xhr = Ti.Network.createHTTPClient({
        onload: function (e) { // this function is called when data is returned from the server and available for use
            // this.responseText holds the raw text return of the message (used for text/JSON)
            // this.responseXML holds any returned XML (including SOAP)
            // this.responseData holds any returned binary data
            Ti.API.debug(this.responseText);
            alert(xhr.responseText);
        },
        onerror: function (e) { // this function is called when an error occurs, including a timeout
            Ti.API.debug(e.error);
            alert(e.toString);
        },
        timeout: 5000 /* in milliseconds */
    });
    xhr.autoEncodeUrl = false;
    var params = {
        email: $.email.value,
        password: $.password.value
    };
    xhr.open('POST', url);
    xhr.send(params); // request is actually sent with this statement 
    Ti.API.info(xhr.responseText);
};

But I can't do it because receive strange message. I put it in the Logs. Besides, I did another requests and they worked successfully. Logs:

[ERROR] : TiHttpClient: (TiHttpClient-1) [3529,3529] HTTP Error (org.apache.http.client.HttpResponseException): Not Found
[ERROR] : TiHttpClient: org.apache.http.client.HttpResponseException: Not Found
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:258)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:217)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1287)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:841)

Solution

  • I solved it with JSON.stringify(params) with the sending the request :

    function loginClick(e)
    {
    var url = 'http://...';
    var xhr = Ti.Network.createHTTPClient({
        onload: function(e) {
            // this function is called when data is returned from the server and available for use
            // this.responseText holds the raw text return of the message (used for text/JSON)
            // this.responseXML holds any returned XML (including SOAP)
            // this.responseData holds any returned binary data
            Ti.API.debug(this.responseText);
            alert(xhr.responseText);
        },
        onerror: function(e) {
            // this function is called when an error occurs, including a timeout
            Ti.API.debug(e.error);
            alert(this.status);
            alert("error" + e.toString);
        },
        timeout:5000  /* in milliseconds */
    });
    xhr.autoEncodeUrl = false;
    var params = {  
        'email': $.email.value,  
        'password' :$.password.value
    };
    
    xhr.open('POST', url);
    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xhr.send(JSON.stringify(params)); // request is actually sent with this statement
    
    };