java.lang.ClassCastException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • Having some issues with Android file upload where the key in my payload is an integer. Sample code to reproduce, taken from Ti API HttpClient file upload docs: {code:java} Titanium.Media.openPhotoGallery({ success: function(event) { var xhr = Titanium.Network.createHTTPClient(); xhr.onload = function(e) { Ti.UI.createAlertDialog({ title: 'Success', message: 'status code ' + this.status }).show(); }; xhr.open('POST', 'REQUESTBI.N URL'); xhr.send({1: event.media}); } }); {code} 1. Create a Requestbi.n url and replace it in the .open() 2. run this on an Android device. You should receive the following error, specifying that the int key cannot be cast to a string, and you will not be able to upload the file to requestbin. [WARN] : W/System.err: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String [WARN] : W/System.err: at ti.modules.titanium.network.TiHTTPClient.send(TiHTTPClient.java:1012) [WARN] : W/System.err: at ti.modules.titanium.network.HTTPClientProxy.send(HTTPClientProxy.java:154) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:70) [WARN] : W/System.err: at android.os.Handler.handleCallback(Handler.java:739) [WARN] : W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) [WARN] : W/System.err: at android.os.Looper.loop(Looper.java:145) [WARN] : W/System.err: at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:118) 3. as suggested by AppC support, change the .send() to {code:java} xhr.send(JSON.stringify({1: event.media})); {code} this will fire and upload to requestbin, however if you look at the response in requestbin, the event.media is a json string and not a binary object. 4. change the .send() to {code:java} var payload = {}; var key = '\"' + 1 + '\"'; payload[key] = event.media; xhr.send(payload); {code} this will post the photo and be received as a binary object in requestbin. I have witnessed this for Android only. IOS seems to handle things ok, and I can post the object without having to do any additional processing. I am posting to an API that has been used by our native Android developers, and they are sending the key as an integer. I believe that the Titanium SDK is somehow tripping over the integer to string conversion Please let me know if you need any more info. Thanks Eric
    via by eric harms,
  • Having some issues with Android file upload where the key in my payload is an integer. Sample code to reproduce, taken from Ti API HttpClient file upload docs: {code:java} Titanium.Media.openPhotoGallery({ success: function(event) { var xhr = Titanium.Network.createHTTPClient(); xhr.onload = function(e) { Ti.UI.createAlertDialog({ title: 'Success', message: 'status code ' + this.status }).show(); }; xhr.open('POST', 'REQUESTBI.N URL'); xhr.send({1: event.media}); } }); {code} 1. Create a Requestbi.n url and replace it in the .open() 2. run this on an Android device. You should receive the following error, specifying that the int key cannot be cast to a string, and you will not be able to upload the file to requestbin. [WARN] : W/System.err: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String [WARN] : W/System.err: at ti.modules.titanium.network.TiHTTPClient.send(TiHTTPClient.java:1012) [WARN] : W/System.err: at ti.modules.titanium.network.HTTPClientProxy.send(HTTPClientProxy.java:154) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:70) [WARN] : W/System.err: at android.os.Handler.handleCallback(Handler.java:739) [WARN] : W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) [WARN] : W/System.err: at android.os.Looper.loop(Looper.java:145) [WARN] : W/System.err: at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:118) 3. as suggested by AppC support, change the .send() to {code:java} xhr.send(JSON.stringify({1: event.media})); {code} this will fire and upload to requestbin, however if you look at the response in requestbin, the event.media is a json string and not a binary object. 4. change the .send() to {code:java} var payload = {}; var key = '\"' + 1 + '\"'; payload[key] = event.media; xhr.send(payload); {code} this will post the photo and be received as a binary object in requestbin. I have witnessed this for Android only. IOS seems to handle things ok, and I can post the object without having to do any additional processing. I am posting to an API that has been used by our native Android developers, and they are sending the key as an integer. I believe that the Titanium SDK is somehow tripping over the integer to string conversion Please let me know if you need any more info. Thanks Eric
    via by eric harms,
    • java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at ti.modules.titanium.network.TiHTTPClient.send(TiHTTPClient.java:1012) at ti.modules.titanium.network.HTTPClientProxy.send(HTTPClientProxy.java:154) at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method) at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57) at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43) at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:70) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:118)
    No Bugmate found.