Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20583] Android: TiHTTPClient.getAllResponseHeaders() throws NullPointerException on timeout

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2016-03-23T08:33:33.000+0000
Affected Version/sn/a
Fix Version/sRelease 5.4.0
ComponentsAndroid
Labelsandroid, httpclient, timeout
ReporterMichael Gangolf
AssigneeHieu Pham
Created2016-03-14T08:04:16.000+0000
Updated2016-07-14T23:04:17.000+0000

Description

Being on 2G my http request will timeout and bringing up the following error message:
 (TiHttpClient-3) [96625,96625] HTTP Error (java.net.SocketTimeoutException): java.net.SocketTimeoutException
 java.net.SocketTimeoutException
 	at java.net.PlainSocketImpl.read(PlainSocketImpl.java:484)
 	at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
 	at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
 	at com.android.okhttp.okio.Okio$2.read(Okio.java:135)
 	at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
 	at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
 	at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
 	at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
 	at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
 	at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
 	at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
 	at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
 	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)
 	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:408)
 	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaders(HttpURLConnectionImpl.java:170)
 	at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:212)
 	at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:543)
 	at java.net.URLConnection.getContentLength(URLConnection.java:315)
 	at ti.modules.titanium.network.TiHTTPClient.handleResponse(TiHTTPClient.java:159)
 	at ti.modules.titanium.network.TiHTTPClient.access$1200(TiHTTPClient.java:85)
 	at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1207)
 	at java.lang.Thread.run(Thread.java:818)
 java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.Map.isEmpty()' on a null object reference
 	at ti.modules.titanium.network.TiHTTPClient.getAllResponseHeaders(TiHTTPClient.java:615)
 	at ti.modules.titanium.network.HTTPClientProxy.getAllResponseHeaders(HTTPClientProxy.java:99)
 	at org.appcelerator.kroll.runtime.v8.V8Object.nativeCallProperty(Native Method)
 	at org.appcelerator.kroll.runtime.v8.V8Object.callProperty(V8Object.java:73)
 	at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1155)
 	at android.os.Handler.dispatchMessage(Handler.java:98)
 	at android.os.Looper.loop(Looper.java:168)
 	at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:118)
 E/TiExceptionHandler(7403): (main) [59,96684] ----- Titanium Javascript Runtime Error -----
 E/TiExceptionHandler(7403): (main) [0,96684] - In ti:/console.js:25,62
 E/TiExceptionHandler(7403): (main) [0,96684] - Message: Uncaught Error: Attempt to invoke interface method 'boolean java.util.Map.isEmpty()' on a null object reference
 E/TiExceptionHandler(7403): (main) [1,96685] - Source: 				( arg.hasOwnProperty('toString') ? arg.toString() : JSON.stringify(arg) ) 
The problem is here: https://github.com/appcelerator/titanium_mobile/blob/dcd73f54156cdd0a9f40ae9a2339ccb68e8c216e/android/modules/network/src/java/ti/modules/titanium/network/TiHTTPClient.java#L615 responseHeaders is null so isEmpty() can't be called Test code:
var url = "http://my.server.com";
var client = Ti.Network.createHTTPClient({
    onload : function(e) {
        Ti.API.info("Received text: " + this.responseText);
        alert('success');
    },
    onerror : function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout : 5000
});

client.open("GET", url);
client.send();
I've changed to 2G and went into a room with bad reception to force the timeout

Comments

  1. Michael Gangolf 2016-03-16

    5.4.0: https://github.com/appcelerator/titanium_mobile/pull/7855 5.3.x: https://github.com/appcelerator/titanium_mobile/pull/7857 5.2.x: https://github.com/appcelerator/titanium_mobile/pull/7856 "Titanium Javascript Runtime Error" alert won't appear. It will just go into the error branch after timeout Tested on HTC A9, Android 6
  2. Ashraf Abu 2016-03-23

    PR Master 5.4.0: https://github.com/appcelerator/titanium_mobile/pull/7855 (merged) 5.3.x: https://github.com/appcelerator/titanium_mobile/pull/7857 (PR Closed, Not Merged) 5.2.x: https://github.com/appcelerator/titanium_mobile/pull/7856 (PR Closed, Not Merged)
  3. Lokesh Choudhary 2016-07-14

    Verified the fix. No null pointer exception thrown. Closing. Environment: Appc Studio : 4.7.0.201607111053 Ti SDK : 5.4.0.v20160713141635 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.5 Appc NPM : 4.2.8-1 Appc CLI : 5.4.0-33 Node: 4.4.4 Nexus 6 - Android 6.0.1

JSON Source