Android Question OKHttpUtils2 Hard Refresh?

moinamh

Member
Hello Community,
Well, I was working with PHP files for a project on the server side, when I ran into a problem.
After changing the PHP file or changing MYSQL, the string received from the server as a Result is the same as the previous string and has not changed.
Due to the caching of information by browsers, this happens in Firefox and Chrome on the desktop too, but with a Hard Refresh using CTRL + F5 , new information is received from the server.
Is there such a possibility in OKHttpUtils2 ? If not, what is the alternative way?
Thanks in advance.
 

drgottjr

Expert
Licensed User
Longtime User
you didn't say whether you were experiencing the same problem with okhttputils2. are you?
or were you simply asking out of curiosity?

if you believe you are, it could be on the server's end. (it could also be on your end, and
having nothing to do with okhttputils2.) your details were a little vague.

if you are experiencing the same problem with okhttputils2, you could attempt to instruct the
server regarding caching (eg,"Cache-Control: no-cache"). caching can occur on both ends of
the request. okhttputils2 supports sending request headers. it also supports seeing what
the server's instructions regarding caching are. it might help you to determine what your
server is doing and what you need to do.
 
Upvote 0

moinamh

Member
OkHttpUtils2 never caches anything.

But it uses the browser engine, doesn't it?

you didn't say whether you were experiencing the same problem with okhttputils2. are you?
or were you simply asking out of curiosity?
Yes, and that's why I'm asking, Let me give an example :

In the user database in MYSQL, there is a column for 'authenticated', and users must be authenticated to log in, and the program reads the result of the request from PHP when logging in, if it is 1, it will allow the user to log in, if it is 0 It asks the user to contact the administration to confirm his/her account ; On the other hand, in the admin program, the list of unapproved users is displayed for approval or rejection, and the administrator can reject or approve users.


The problem that occurs is that if the verified user wants to enter his account immediately (up to 5 minutes) after being verified by the admin, he will receive the previous value (0).
A strange thing that I came across during unit testing, if I hard refresh the same PHP page in a computer browser, for example Firefox, the information is updated both in the browser and in the program that uses OkHttpUtils to get the information!
Now, if after the user's approval, the PHP file is opened with a browser like Edge that has never opened that PHP file before, it will display the new value (1) without any problem, but OkHttpUtils will still display the previous value!
This problem can be seen in various sections that need to be updated quickly, such as the list of offers and news.

(eg,"Cache-Control: no-cache")
I would be grateful if you could give an example of this.
Because I tried using Headers(Found some examples in community) and Accept_All (it was a shot in the dark, I know) in OkHttpUtils and the result is the same both in Release and Debug mode.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you don't have to use a "browser engine" to request something from a server.
a browser formulates a request following the http protocol and renders the
response as a "web page". if you simply want to request something from a
webserver, you can either code the request yourself (following the protocol)
or you can use an http client (which handles the protocol for you). that's what
okhttputils2 is/does.

you say:
Because I tried using Headers(Found some examples in community) ...
just send a
B4X:
job.getrequest.setheader("Cache-Control","no-cache")
header as you would for other headers.

you don't show any code, we don't know how you've implemented anything, we
don't know exactly what the server is expecting or how it responds. we only have
your understanding of what is supposed to happen. and we only have your
understanding of how okhttputils2 works, which was not correct.

there are a number of things a browser does by design that are not automatically
done by okhttputils2, which is not to say that okhttputils2 cannot do them. unless
the problem is evident by glancing at your code, we need a way to run the code
ourselves.
 
Upvote 0

moinamh

Member
Thank you for the information you provided.

And I implemented the "Cache-Control" header, but I need to test in different sections. In the initial login test, it receives the "new" information successfully.
I will announce the results of more tests for people who may encounter a similar problem.

you don't show any code, we don't know how you've implemented anything, we
don't know exactly what the server is expecting or how it responds. we only have
your understanding of what is supposed to happen.
And I didn't have a question about how to Code it, I had a question about the feature
Because even if you test the code, I can't apply the changes exactly 30 seconds before you check it.
but still, this is my code.

checkVerfied:
    Sleep(1)
    Dim job2 As HttpJob
    Log("Entering job Check Verfied...")
    job2.Initialize("checkVerfied", Me)
    job2.Download2(AWA.strURL & "checkVerfied.php" , _
    Array As String("uid",User.ID))
    job2.getrequest.setheader("Cache-Control","no-cache")
    wait for (job2) JobDone(job2 As HttpJob)
    If job2.Success Then
        Dim strReturn As String = job2.GetString
        Log(job2.GetString)
    Else
        Log("Error: " & job2.ErrorMessage)
        ToastMessageShow("Error: " & job2.ErrorMessage, True)
    End If
    job2.Release
    If strReturn.Contains("<meta charset=") Then AWA.FixString(strReturn)
    Return IIf(strReturn = "1",True,False)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, cache-control worked? is that what you're saying?

as to posting code, you may have no questions, but
anyone trying to help you will want to know how you
implement what you say you're implementing.
nothing personal, just business.
 
Upvote 0

moinamh

Member
so, cache-control worked? is that what you're saying?
Yes, Thank You! ? I haven't tried this Header before.
For anyone who has encountered a similar problem and is reading this thread,
The "Cache-control" Header , in most cases updates the immediate changes, So give it a shot!

But why "in most cases"?
So far, in some parts of the program, the results of the changes are displayed instantly (such as the "User Verification" section), but in some parts, such as "News" Section, the received JSON is not updated with changes.
As if it should be Hard-Refreshed by a browser ( on some PC somewhere ), or 5 to 10 minutes after applying changes, the App should be completely closed and reopened to receive new information.
However, the received JSON of the "Suggestions" section does not have this problem and receives new information after instant change with the "Cache-Control" option.

This is a strange problem that I encountered 2 years ago too, but since the project was canceled, I did not follow up on it then.
I left a ticket for the host provider. Maybe the problem is on their side (but why was the problem solved in some parts of the program?)

but
anyone trying to help you will want to know how you
implement what you say you're implementing.
nothing personal, just business.
I know and I honestly appreciate your time and answers.
All I'm saying is, all the questions in this forum may not be completely related to the code.
And not all "Unlicensed" Members are new to the forum and B4X Language.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…