Android Question String comparison

Discussion in 'Android Questions' started by davemorris, Aug 9, 2019.

  1. davemorris

    davemorris Member Licensed User

    Hi, Guys
    I have a problem with string comparison, the line
    IF fcmToken = webFcmToken then always fails (see code below).

    I have viewed tokens with in debug an they appear identical. Also tried .trim and even .toupper but still failed the comparison.

    Also tried fcmToken.compare(webFcmToken) and got the result 67!

    I think it is something to do with encoding - fcmToken is taken from my code after it calling firebasemessaging.SubscribeToTopic("general") and the other (webFcmToken) is from web based database.

    Example code below - I have marked the problem statement with <<<< ALWAYS FAILS
    Code:
    ' Query update FCM token - Returns true if FCM tokens are the same or the FCM token updated successfully.
    private Sub QueryUpdateFcm(customerId As Int, fcmToken As String)As ResumableSub
        
    Dim fcmUpdatedOk As Boolean = False
        
    Dim jobGet As HttpJob: jobGet.Initialize("UseWebApi", Me)
        jobGet.Download(modEposWeb.URL_CUSTOMER_API & 
    "/" & customerId & "?" & modEposWeb.API_QUERY & "=" & modEposWeb.API_GET_FCMTOKEN)
        
    wait for (jobGet) jobDone(jobGet As HttpJob)
        
    If jobGet.Success And jobGet.Response.StatusCode = 200 Then
            
    Dim webFcmToken As String = jobGet.GetString
            
    If fcmToken = webFcmToken Then ' <<<< ALWAYS FAILS
                fcmUpdatedOk = True
            
    Else
                
    Dim jobPut As HttpJob: jobPut.Initialize("UseWebApi", Me)
                jobPut.PutString(modEposWeb.URL_CUSTOMER_API & 
    "/" & customerId & "?" & modEposWeb.API_SETTING & "=" & _
                modEposWeb.API_GET_FCMTOKEN & 
    "&" & modEposWeb.API_SETTING_1 & "=" & fcmToken, "")
                
    Wait For (jobPut) jobDone (jobPut As HttpJob)
                
    If jobPut.Success And jobPut.Response.StatusCode = 200 Then
                    fcmUpdatedOk = 
    True
                
    End If
                jobPut.Release
            
    End If
        
    End If
        jobGet.Release
        
    Return fcmUpdatedOk   
    End Sub
    Any ideas?

    Kind regads
    Dave
     
  2. DonManfred

    DonManfred Expert Licensed User

    Do some tests
    convert them to hexcode and log the two hexstrings.
    You may get an idea what is different.

    Probably the string coming from your webservice is not in UTF8.
     
  3. davemorris

    davemorris Member Licensed User

    Hi
    I did insert a line "jobGet.GetRequest.SetHeader("Accept-Encoding","utf8")" but that did not work.

    Code:
    ' Query update FCM token - Returns true if FCM tokens are the same or the FCM token updated successfully.
    private Sub QueryUpdateFcm(customerId As Int, fcmToken As String)As ResumableSub
        
    Dim fcmUpdatedOk As Boolean = False
        
    Dim jobGet As HttpJob: jobGet.Initialize("UseWebApi", Me)
        jobGet.Download(modEposWeb.URL_CUSTOMER_API & 
    "/" & customerId & "?" & modEposWeb.API_QUERY & "=" & modEposWeb.API_GET_FCMTOKEN)
        jobGet.GetRequest.SetHeader(
    "Accept-Encoding","utf8"' <<<< INSERTED TO ENCODE TO UTF-8 (don't work either)
        wait for (jobGet) jobDone(jobGet As HttpJob
        
    If jobGet.Success And jobGet.Response.StatusCode = 200 Then
            
    Dim webFcmToken As String = jobGet.GetString
            
    If fcmToken = webFcmToken Then ' <<<< ALWAYS FAILS
                fcmUpdatedOk = True
            
    Else
                
    Dim jobPut As HttpJob: jobPut.Initialize("UseWebApi", Me)
                jobPut.PutString(modEposWeb.URL_CUSTOMER_API & 
    "/" & customerId & "?" & modEposWeb.API_SETTING & "=" & _
                modEposWeb.API_GET_FCMTOKEN & 
    "&" & modEposWeb.API_SETTING_1 & "=" & fcmToken, "")
                
    Wait For (jobPut) jobDone (jobPut As HttpJob)
                
    If jobPut.Success And jobPut.Response.StatusCode = 200 Then
                    fcmUpdatedOk = 
    True
                
    End If
                jobPut.Release
            
    End If
        
    End If
        jobGet.Release
        
    Return fcmUpdatedOk   
    End Sub
    Have you provide code snippet for converting a string to hexcode?

    Kind regards
    Dave
     
  4. DonManfred

    DonManfred Expert Licensed User

    The forumsearch IS working. I´m sure it would reveal the library used (Byteconverter)...

    Do not expect everytime someone do the search for you

    Code:
    Log($"Str1 = ${str1}, hex = ${bc.HexFromBytes(str1.GetBytes("UTF8")}"$)
        
    Log($"Str2 = ${str2}, hex = ${bc.HexFromBytes(str2.GetBytes("UTF8")}"$)
     
    Last edited: Aug 9, 2019
  5. davemorris

    davemorris Member Licensed User

    Hi

    Firstly, in my defense, can I just say I did attempt to use the forum search - looking for "convert string to hexcode" and get hits (but none very useful) and "Byteconverter" is a long way from my search parameters - you must appreciate with search engines it helps if you know exactly what you are looking for and when libraries are involved, I find it can be very misleading.

    Anyway, thanks for the heads up - the hex dump did show the problem. It appeared jobGet.GetString returns the string between quotes. When using the debug to display the string, it shown without quotes. A simple log report did show the quotes - So Beware of the debug when display strings!

    i.e. fcmToken shown in debug as adkdfd..... and log(fcmToken) shown as "adkdfd...."

    I have checked the http message from the Server and no quotes are included in the response. I think if I start asking about the GetString() operation or the issue with debug and log reports that needs to be for another thread (obviously after I have done some searching).

    Kind regards
    Dave
     
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