Android Question Http Poststring JSON URL Encoding

Discussion in 'Android Questions' started by lip, Feb 7, 2015.

  1. lip

    lip Active Member Licensed User

    I am able to transfer SQLite records one at a time with HTtP using Posstring with a string like
    iD=1&name=David

    My co-developer at the server end is using .net and he reads the Post and inserts/updates a SQL record.

    My problem comes when I try to use JSON ie something like:
    Username=user&password=password&UploadJSON=[{ID:1},{name:David}]

    I have used log(myPoststring) immediately before Job1.Poststring(myurl,myPoststring) and I see the JSON object included in the string as above

    However, at the server end we log the received string as:-
    Username=user&password=password&UploadJSON=%22%7ID%141%8......%23

    We cannot work out where, how or why the JSON part of the string is being url encoded when the first part of the string is not being encoded.

    I realise that this could be happening at the server end but my web developer is adamant that this is how the string is received.

    Any advice, or ammunition, much appreciated
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    HttpUtils2 (or HttpClient) doesn't modify the message in any way.

    Have you tried to url-encode the json string with StringUtils?
     
  3. KMatle

    KMatle Expert Licensed User

    Are you using UTF8 on the server side?
     
  4. lip

    lip Active Member Licensed User

    I am using the JSonGenerator. Do I need StringUtils tool?


    Code:
    Sub CloudSyncDevice As Int
       
        
    'Record Sync Time
        Dim LastSynced As Long
            LastSynced = Subs.GetParamIntLong(
    "LastSyncDateDevice")
        
    Dim DeviceToSyncC As Cursor
            DeviceToSyncC = Main.WiE3SQL.ExecQuery(
    "SELECT DeviceSN, Drawing, PosTop, PosLeft, DeviceType, ApplicationID, ConfigByte, ConfigOptions, MinDaylightLevel FROM Device WHERE DeviceSN > 1000 AND LastEdit > " & LastSynced)
        
    If DeviceToSyncC.RowCount = 0 Then
            DeviceToSyncC.Close
            
    Return 0
        
    Else
            
    'Create a List of Device Maps
            Dim DeviceList As List
                DeviceList.Initialize
            
    For i = 0 To DeviceToSyncC.RowCount - 1
                DeviceToSyncC.Position = i
                
    Dim DeviceRecord As Map
                    DeviceRecord.Initialize
                    DeviceRecord.Put(
    "DeviceSN",DeviceToSyncC.GetInt("DeviceSN"))
                    DeviceRecord.Put(
    "Drawing",DeviceToSyncC.GetString("Drawing"))
                    DeviceRecord.Put(
    "PosTop",DeviceToSyncC.GetInt("PosTop"))
                    DeviceRecord.Put(
    "PosLeft",DeviceToSyncC.GetInt("PosLeft"))
                    DeviceRecord.Put(
    "DeviceType",DeviceToSyncC.GetInt("DeviceType"))
                    DeviceRecord.Put(
    "DeviceTypeID",DeviceToSyncC.GetInt("ApplicationID"))
                    DeviceRecord.Put(
    "ConfigByte",DeviceToSyncC.GetInt("ConfigByte"))
                    DeviceRecord.Put(
    "ConfigOptions",DeviceToSyncC.GetInt("ConfigOptions"))
                    DeviceRecord.Put(
    "MinDaylightLevel",DeviceToSyncC.GetInt("MinDaylightLevel"))
                DeviceList.Add(DeviceRecord)
            
    Next
           
            
    'Create the JSON Object
            Dim DeviceJSON As JSONGenerator
                DeviceJSON.Initialize2(DeviceList)
           
            
    If Main.CloudConnected AND Main.CloudEnabled Then
                
    ToastMessageShow("JSON for " & DeviceToSyncC.RowCount & " Devices Sent to " & "DeviceUpload.aspx",False)
                
    Dim myTimeStored As Long
                    myTimeStored = 
    DateTime.Now
               
                
    'Create String to Post
                Dim myPostString As String
                    myPostString = 
    "PostCode=" & Main.ActivePostcode & _
                    
    "&UserID=1" & _
                    
    "&Collator=" & Main.AndroidID & _
                    
    "&TimeStored=" & myTimeStored & _
                    
    "&DeviceJSON=" & DeviceJSON.ToString
               
                
    'Initialise HTTP Job
                Dim Job1 As HttpJob
                    Job1.Initialize(
    "JobUploadDeviceJSON",Me)
                    
    'Default timeout is 30 seconds.  Use line below to change it
                    'Job1.GetRequest.Timeout = 30000
                Log(myPostString)
               
                
    'Post the String inc JSON
                Job1.PostString(Main.HTTPServer & "DeviceUpload.aspx",myPostString)
            
    End If
           
            
    'Get Sync Count before closing cursor
            Dim myDevicesSynced As Int
                myDevicesSynced = DeviceToSyncC.RowCount
           
            
    'Close Cursor and Return
            DeviceToSyncC.Close
            
    Return myDevicesSynced
        
    End If
    End Sub
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Your code is correct. The problem is probably in the server configuration. HttpJob.PostString doesn't modify the payload.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice