Android Question Double Posting Request

Calvin Yee

Member
Licensed User
Hi,
I'm trying to implement posting data from my device to a Web API.

My facing problem is my post request is executed twice posting for 1 request due to slow network.

do u have any function to disable the auto retry feature of OkHttp.

please advice.

example code as following I call web API:-

B4X:
Dim json As HttpJob
            json.Initialize("MyJobUpdateResult",Me)
            Dim m As Map
            m.Initialize
            m.Put("DocNo",lblISno.Text)
            m.Put("SalesNo",lblISsalesno.Text)
            m.Put("DocStatus",DocStatus)
            m.Put("ActionDate",actiondate)

Dim JSONGenerator As JSONGenerator
            JSONGenerator.Initialize(m)
            Dim JSONstring As String
            JSONstring = JSONGenerator.ToString
            Log(JSONstring)
json.PostString(Starter.URILink & "/api/Doctor/PostICS",JSONGenerator.ToString)
            json.GetRequest.SetHeader("Authorization","Bearer " & Main.MyToken)
            json.GetRequest.SetContentType("application/json")
            json.GetRequest.Timeout = 60000
            Wait For (json) JobDone(json As HttpJob)
            If json.Success Then
                Log(json.GetString)
                Dim jNDparser As JSONParser
                jNDparser.Initialize(json.GetString)
                Dim root As Map = jNDparser.NextObject
                Dim result As String = root.Get("result")
                Dim data As String = root.Get("data")
                Dim resultset As String = root.Get("resultset")
                'If data.Trim ="Successful" Then
                If result.Trim ="1" Then
                    str="UPDATE TaskList SET SyncResult='Y' "
                    str=str & " WHERE Docno='" & lblISno.Text & "' "
                    SQL1.ExecNonQuery(str)
                    
                    
                    
                    New_ISRNo=data.Trim 
                End If
            Else
            End If
            json.Release
 

Alexander Stolte

Expert
Licensed User
Longtime User
Now I understand this maybe the reason why I have duplicate users registered to my app.
But normally you have a primary key over these columns in the database to avoid that a user can register multiple times.
Or a unique property.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I have no issue with duplicate. When the select user query is executed, only the first record is used. The second record will be redundant.
But what sense does that make?
This is unnecessary data that only takes up space. And if a user deletes his account, this data must be deleted as well. If the user changes his email address, password, or whatever, this data must be maintained, otherwise it is inconsistent. ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
But what sense does that make?
This is unnecessary data that only takes up space. And if a user deletes his account, this data must be deleted as well. If the user changes his email address, password, or whatever, this data must be maintained, otherwise it is inconsistent. ?
Ya, I will see how I will fix it, such as using unique column like you mentioned. This happen in my old app where I am not good in writing PHP.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Inside the okhttp lib, there is another function "initializePost" which supposingly will not retry upon I/O failure, you can alter the original HttpJob Code by including this type of functionality, but I can't be sure if this helps either. Details here.
However, server side should always have a way to deal with such things. If it's your own server, you could for e.g. add a random key generated at your client, send it with your request, and if server encounters a duplicate, it will omit the insert.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Ya, I will see how I will fix it, such as using unique column like you mentioned. This happen in my old app where I am not good in writing PHP.
IMPORTANT: Backup your database first!

Check duplicate records:
SQL:
SELECT
    `user_email`,
    COUNT(`user_email`)
FROM
    tbl_users
GROUP BY `user_email`
HAVING COUNT(`user_email`) > 1

Update duplicate records to NULL
SQL:
UPDATE `tbl_users` t2
INNER JOIN `tbl_users` t1 ON t1.`user_email` = t2.`user_email`
SET t2.`user_email` = NULL
WHERE t1.`id` < t2.`id`
--AND t2.`id` < 300 #Remarks: This line is optional, I want to test apply for a few records first

or you can just delete if confirm don't need the redundant records
SQL:
DELETE FROM `tbl_users` t2
INNER JOIN `tbl_users` t1 ON t1.`user_email` = t2.`user_email`
WHERE t1.`id` < t2.`id`

Alter the table to set user_email as Unique column:
SQL:
ALTER TABLE `tbl_users` ADD UNIQUE(`user_email`);
or use PhpMyAdmin action
1590225302079.png
 
Upvote 0
Top