Android Tutorial HttpUtils2 - Web services are now even simpler

Martin Larsen

Active Member
Licensed User
Longtime User
Good news: httputils2 does in fact maintain cookies across downloads! There as something wrong with my second call, after the login

But I have a found a new problem with httputils2. If a job fails because of an error such as 404, it's is not possible to get the response string with Job.GetString. This should really be possible since the returned document often explains in more details what's wrong.
 

Martin Larsen

Active Member
Licensed User
Longtime User
1. You can send a HEAD request. How large is the string?

About 120 bytes at most. It's not that it doesn't work with httputils2, it would just be handy with a way to immediate download some data and assign it to a string in one call, just like file_get_contents in php can do.

If Job.Success is true then the response code is 2xx. You can get the status code in hc_ResponseSuccess (or ResponseError) in HttpUtils2Service.

Thanks, I will look into that.

And yes, I found that cookies are indeed maintained, and it works great!

B4A is really awesome
 

aeric

Expert
Licensed User
Longtime User
Hi B4A members, I created a sample log in using HttpUtils2 connect to a MySQL table by php script. I want to share it. May I create a new thread in "Tutorials & Examples" to share the code?
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
We can use APS.NET web service with url or execute method in url
But in PHP we cannot execute method in url,we must be use soap
Is there any way to use HttpUtil2 to web service and is threre example for php?
 

ilan

Expert
Licensed User
Longtime User
i have a small problem

when i download a file everything is ok but after changing the text on the ftp server i get in job.done the old text and not the updated text

if i put the textfile link in chrome i see the updated text but on my app i get the old text in job.Getstring also after i release the job(job.Release)...

what can i do??
 

ilan

Expert
Licensed User
Longtime User
Did you use the same httpjob or are you DIMming a new one each time?
MAybe show some example code which shows the problem (small testproject)

i use the same
B4X:
       Dim job3 As HttpJob
    job3.Initialize("Job3", Me)
    job3.Download(newftp2 & "/lists/" & link)

should i maybe clear File.DirInternalCache?
 

ilan

Expert
Licensed User
Longtime User
Try using a NEW
B4X:
Dim job3 As HttpJob
each time you start a download. Dont reuse the old object.

this is not working

the thing is that after a while i will get the updates string but not immidiatly after i changed it
maybe something with the ftp server?? (but i can see the new string in google chrome)

this is the link: http://www.sagital.byethost7.com/lists/0504844663.txt

updated string:

ענבים|0004|פירות וירקות|יח|9.90| | |C
חלב|0001|חלב, ביצים וסלטים|ליטר|8.90| | |A

what i get (old string):

 

ilan

Expert
Licensed User
Longtime User
if i download the file via ftp i see the new string but with Http job not

this is working:
FTP.DownloadFile("/htdocs/lists/" & LineNumber & ".txt", False, File.DirInternal, LineNumber & ".txt")

this not:
Dim job3 AsHttpJob
job3.Initialize("Job3", Me)
job3.Download(newftp2 & "/lists/" & link)
 

ilan

Expert
Licensed User
Longtime User
now i have deleted the file from my server but its still downloading it (the old string)

there is a chache folder maybe where its downloading from it instead from the ftp server...
 

ValDog

Active Member
Licensed User
Longtime User
I am posting data (JSON) to a server which requires an "authorization header like: Authorization Bearer [your_access_token]."

The relevant code is as follows:

job4.Initialize("Job4", Me)
job4.PostString(PostUrl, gen.ToString)
job4.GetRequest.SetHeader("Authorization Bearer", AccessToken)

When I execute I get:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>

JobName = Job4, Success = false



Am I using the SetHeader method correctly? Any idea what may be my problem, or how to proceed with troubleshooting it?
 

ValDog

Active Member
Licensed User
Longtime User
Yes, you are setting the header correctly.

Can you access this site with another client (browser for example)? If yes then you can monitor the network traffic and see the request that it sends.
I believe I found my problem - thanks!
 

marfonte

New Member
Licensed User
Longtime User
Hi,

How do I call an http verb patch? I need to request an oData update entity

Thanks,
Mario
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…