iOS Question HTTPJOB works fine in B4A but not B4I

touchsquid

Active Member
Licensed User
Longtime User
This code works correctly in B4A (The data appears in the remote database). In B4a I get a correct result string. In B4I I get the job1.errormessage "Request completed with error". The library is iHttpUtils2. The XML File is the same for both B4A and B4i. I have access to the back end server (ASMX) and I added code to send me an email when the Sub is accessed. I get an email when I use the b4A app, none from B4I. So I surmise there is something wrong with the request. Any ideas what could be wrong?

One more thing. I have several other routines using nearly identical code, accessing different calls within the same service, and they all work.

B4X:
Sub AddBoat(NB As Boat)As ResumableSub
	Dim Result As String
	Dim req As String = File.ReadString(File.Dirassets, "addboat.xml")
	req=SetParam("ID",NB.ID,req)
	req=SetParam("User",NB.user,req)
	req=SetParam("Password",NB.Password,req)
	req=SetParam("BoatName",NB.BoatName,req)
	req=SetParam("BoatClass",NB.BoatClass,req)
	req=SetParam("YC",NB.YC,req)
	req=SetParam("SailNum",NB.SailNum,req)
	req=SetParam("PHRF",NB.PHRF,req)
	req=SetParam("LOA",NB.loa,req)
	req=SetParam("Skipper",NB.Skipper,req)
	req=SetParam("Email",NB.Email,req)
	req=SetParam("Phone",NB.Phone,req)
	'Mydata.LogMD(NB.id)
	Dim job1 As HttpJob
	
	job1.Initialize("AddBoat", Me)
	Dim URL As String = Connect
	Try
		job1.PostString(URL, req)
		job1.GetRequest.SetContentType("text/xml;  charset=utf-8")
		
		Wait For (job1) JobDone(job1 As HttpJob)
		If job1.Success Then
			Result= job1.getstring
		Else
			Mydata.logmd(job1.ErrorMessage) 'logs to a file
		End If
		job1.Release
		Dim BoatID As Int = GetIntParam("AddBoatResult",Result)
		Return BoatID
	Catch
		hd.ToastMessageShow ("No Internet",True)
		job1.Release
		Return -1
	End Try
End Sub

public Sub SetParam(Param As String, Value As String,XML As String ) As String
	If Value = Null Then Value = ""
	Dim SF As String = Param & ">?<"	
	Dim SF2 As String = Param & ">" & Value & "<"
	Return XML.Replace(SF,SF2)	
End Sub

the XML file is as follows:
B4X:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:act="https://ActiveRace.club/">
<soapenv:Header/>
<soapenv:Body>
<act:AddBoat>
<act:newboat>
<act:ID>?</act:ID>
<act:User>?</act:User>
<act:Password>?</act:Password>
<act:BoatName>?</act:BoatName>
<act:YC>?</act:YC>
<act:BoatClass>?</act:BoatClass>
<act:SailNum>?</act:SailNum>
<act:PHRF>?</act:PHRF>
<act:LOA>?</act:LOA>
<act:Skipper>?</act:Skipper>
<act:Email>?</act:Email>
<act:Phone>?</act:Phone>
</act:newboat>
</act:AddBoat>
</soapenv:Body>
</soapenv:Envelope>
 

hatzisn

Expert
Licensed User
Longtime User
URL encode the data you send with stringutils and URL decode it in the receiving part. Pay attention to the fact that [SPACE] character is URL encoded as %20 in B4i and "+" in B4J and B4A. Align to all IDEs this difference by changing in B4i manually after URL Encode.
 
Last edited:
Upvote 0

touchsquid

Active Member
Licensed User
Longtime User
URLencode didnot work for me (possibly the ASP.net webservice expects plain XML text.) However, it led me to a solution.
There was an ampersand in the request, and this caused the problem.

This line of code makes it work:
req=req.Replace("&", "and")

I will put it in other places where the "&" character could be entered by the user.

Thanks for your help!

Grahame
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
URLencode didnot work for me (possibly the ASP.net webservice expects plain XML text.) However, it led me to a solution.
There was an ampersand in the request, and this caused the problem.

This line of code makes it work:
req=req.Replace("&", "and")

I will put it in other places where the "&" character could be entered by the user.

Thanks for your help!

Grahame

Just out of curiocity. I did not understand if you URLEncoded the values that were to be placed in the XML in the place of the "?" or you URLEncoded the full XML String. The correct was the first. Maybe it was ambiguous the way I said it. The ambersand part, I faced it also in the past and that's why I told you to URL encode the data you send.
 
Upvote 0

touchsquid

Active Member
Licensed User
Longtime User
I urlencoded the entire XML string. I will try again with just the data strings URL encoded. Thanks for the clarification!

BTW the & is rare enough that the code is a couple of years old and this is the first time it failed.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I urlencoded the entire XML string. I will try again with just the data strings URL encoded. Thanks for the clarification!

BTW the & is rare enough that the code is a couple of years old and this is the first time it failed.

You must also URL Decode in the WebService part (Uri.UnEscapeDatastring(....)). It would also fail in B4A. It happened to me there.
 
Upvote 0
Top