Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-23282

HttpClient file upload, Android, int as key in payload causes java.lang.Integer cannot be cast to java.lang.String

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Environment:

      OSX Yosemite 10.10.5
      Macbook pro Mid 2015
      Ti SDK 5.2.2.GA
      Android device, tested on multiple different devices > 4.2

      Description

      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:

      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});
          }
      });
      

      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

      xhr.send(JSON.stringify({1: event.media}));
      

      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

      var payload = {};
      var key = '\"' + 1 + '\"';
      payload[key] = event.media;
      xhr.send(payload);
      

      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

        Attachments

          Activity

            People

            • Assignee:
              msamah Ashraf Abu (Inactive)
              Reporter:
              capsizeno4 eric harms
            • Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-9684
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Source Code