Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

caught Ti.Network.createHTTPClient error still causing app to crash

$
0
0

hi, i'm having a weird problem i'm hoping someone can help me with:

i'm using 3.4.0sdk+alloy with genymotion/android 4.1.2.

i've created a basic http client manager that sets up and opens an API call, returns data via a callback. i started this because i was having some issues with connection errors and i was trying to encapsulate the logic into a single spot. i borrowed heavily from this post on stackoverflow.

here is the crux of the problem -- it works perfectly when it receives a response from the API, but if i set the timeout to 1ms (to simulate that no response was received in time, maybe the user is on a bad network? i've already checked to make sure they're online, but who knows, right?) the whole app crashes even though the code seems to be trapping the error.

what gives? am i actually not catching it?

here is my request:

client.sendRequest({
    url: API_URL + 'user/' + USER_ID,
    auth: AUTH_TOKEN,
    done: function(e) {
        Ti.API.info('returned: ' + e.success);
        if (e.success) {
            $.index.close();
            Alloy.createController("main").getView().open();
        } else {
            $.index.close();
            Alloy.createController("login").getView().open();
        }
    }
});
as you can see from the log (further down), according to the second line, the error seems to be (correctly) caught in the onerror function. at the 4th line, it's inside the callback function. seems to be working perfectly.

it just never makes it past there. i'm not completely sure i understand what's happening next, but it seems that despite the catch, the thread just continues to crash..?

segment of the customclient.js:

xhr = Ti.Network.createHTTPClient();
xhr.onload = function(e) {
    //Ti.API.info(this.responseText + ' || ' + this.status);
    if (this.status >= 200 && this.status < 300) {
        // edited down for brevity...
        clientResponse = { success: true, status: this.status, response: JSON.parse(this.responseText) };
    } else {
        clientResponse = { success: false, status: this.status, errormessage: 'Status: ' + this.status };
    }
    if (callback) { callback(clientResponse); }
    cleanUp();
};
xhr.onerror = function(e) {
    Ti.API.info('Exception: ' + e.error);
    if (callback) { callback({ success: false, errormessage: e.error }); }
    cleanUp();
};
xhr.timeout = 1;//0000;
 
xhr.open(method, params.url);
if (params.auth) { xhr.setRequestHeader('Authorization', 'Basic ' + params.auth); }
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(postdata);
as a side note, i tryed adding extra try/catch blocks around the whole segment above, just the first line, just open() line, just the send() line, all of those lines individually, in clumps, ... but no combination had a successful outcome (and some of them were worse, ha!).

excerpt of console log:

[ERROR] :  TiHttpClient: (TiHttpClient-1) [145,145] HTTP Error (java.net.SocketTimeoutException): java.net.SocketTimeoutException
[INFO] :   Exception: java.net.SocketTimeoutException
[ERROR] :  TiHttpClient: java.net.SocketTimeoutException
[INFO] :   returned: false
[ERROR] :  TiHttpClient:    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
[ERROR] :  TiHttpClient:    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
[ERROR] :  TiHttpClient:    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
[ERROR] :  TiHttpClient:    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
[ERROR] :  TiHttpClient:    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
[ERROR] :  TiHttpClient:    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
[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:1328)
[ERROR] :  TiHttpClient:    at java.lang.Thread.run(Thread.java:856)
sorry this is so long, i thought more info would be more helpful. if anyone has any thoughts, i'd love to hear them. thanks!!

Viewing all articles
Browse latest Browse all 8068

Trending Articles