Android Tutorial Posting a byte array to a web service - vb.net example

Hi All,

I've just had a fun time trying to get a webservice to receive a byte array converted from an XML file with data to upload. I'd always used parameters for web service stuff and never needed to upload a file so this was tricky.

In case anyone else has the same issue in the future and doesn't want to do all the leg work here is how you can do it using vb.net web services (note there is an example from someone else on how to do it with a WCF web service - EDIT, here is the link: http://www.b4x.com/forum/basic4andr...s/13603-create-xml-send-wcf-rest-service.html)

In your B4A app do all the normal stuff with HTTPUTILS2SERVICE, code below shows you how to take the stringToUpload and post it in the correct format.

B4X:
Dim syncJob As HttpJob
syncJob.Initialize("UploadJob",  Me)
syncJob.PostBytes( "http://examplewebservice.yourdomain.co.uk/WebServiceFileName.asmx/Upload",stringToUpload.GetBytes("UTF8"))

In your web service the following example will allow it to decode the string and use it. Note that the function shown should return a dataset - you can change to be what you want though.

B4X:
 <WebMethod(Description:="Upload data")> _
   Public Function Upload() As DataSet
        Try
            Dim _httpRequest As HttpRequest = HttpContext.Current.Request
            Dim _requestLength As Long = _httpRequest.InputStream.Length
            Dim _xmlFileAsByteAray() As Byte
            ReDim _xmlFileAsByteAray(_requestLength)
            _httpRequest.InputStream.Read(_xmlFileAsByteAray, 0, Convert.ToInt32(_requestLength))
            Dim _xmlFile As String = System.Text.Encoding.UTF8.GetString(_xmlFileAsByteAray)
            ' Now process the xml file 
        Catch ex As Exception
            ' Send error back to app
        End Try

        ' Now do your return dataset if you need one
    End Function

I did spend at least 30 minutes trying to get this working and to debug I was emailing myself the XML file - sadly for a while I'd forgotten that the email was HTML so didn't see any data, luckily I twigged that the XML probably wouldn't show so viewed the source. Dumbass :)

Hope this helps someone....

Cheers,

Jon
 
Last edited:

joneden

Active Member
Licensed User
Longtime User
Yeah that's the one. I must admit that the XML builder side of that example really didn't work for me anyway - possibly different versions of the library or something...

I'll amend the original post to add in that link - thanks for that!

Regards,

Jon
 
Top