Android Question How can i modify this code to post data to my server

Makumbi

Well-Known Member
Licensed User
how can i modify this code to post data to the server
this example has only one parameter
B4X:
Dim j As HttpJob
    j.Initialize("msg", Me)
    j.PostString(link & "?type=text", txtMessage.Text)

the problem with this code below is sometimes it is slow
B4X:
Dim j As HttpJob
            j.Initialize("", Me)
            j.Download($"http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$ )
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.GetString)
            End If
 

pluton

Active Member
Licensed User
Longtime User
I didn't code in a while in B4A but is it normal that $ sign in


B4X:
j.Download($"http://kccu.........
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
j.PostString(yourURL,"param1=value1&param2=value2&...")
Keep in mind that you also have to urlEncode your parameters and values, using stringUtils.
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
B4X:
j.PostString(yourURL,"param1=value1&param2=value2&...")
Keep in mind that you also have to urlEncode your parameters and values, using stringUtils.
Do you have a nother practical example that i can follow because the one you have posted seems to have little information.
2. Does encoding solve the problem of slowing at posting
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I didn't code in a while in B4A but is it normal that $ sign in
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Exactly like you did in the first post.
I tried it here but it raises an error say it expected
B4X:
Dim j As HttpJob
            j.Initialize("", Me)
            j.PostString($"http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$ )
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.GetString)
            End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm sorry but we cannot help you like this.

On the first post you said: "the problem with this code below is sometimes it is slow"

I answered:
"The number of parameters will have no effect on the performance.
It doesn't matter how you add the parameters."

Now you are talking about an error and you even don't tell us which error. How can we help you?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Note: PostString requires two parameters (see https://www.b4x.com/android/help/httputils2.html#httpjob_poststring):
PostString (Link As String, Text As String) As String

in your code, you only have one (the Link:
http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx} ). You're missing the Text parameter. If it's empty, just use "" (two double quotes, no space between them).
Of course, why then use PostString, maybe you were trying to do
B4X:
Dim link As String = "http://kccug.com/KabojjaApp/RecieveSMS.ashx"
Dim text As String = $"customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$
j.PostString(link, text)
As to this being faster than,
B4X:
j.Download($"http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$ )
which was your initial complaint, that makes no sense. In your case, in my opinion, the speed issue is with the server. The server may be erratic in its response times. Nothing you can do about it on the front end of it.
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Note: PostString requires two parameters (see https://www.b4x.com/android/help/httputils2.html#httpjob_poststring):
PostString (Link As String, Text As String) As String

in your code, you only have one (the Link:
http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx} ). You're missing the Text parameter. If it's empty, just use "" (two double quotes, no space between them).
Of course, why then use PostString, maybe you were trying to do
B4X:
Dim link As String = "http://kccug.com/KabojjaApp/RecieveSMS.ashx"
Dim text As String = $"customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$
j.PostString(link, text)
As to this being faster than,
B4X:
j.Download($"http://kccug.com/KabojjaApp/RecieveSMS.ashx?customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$ )
which was your initial complaint, that makes no sense. In your case, in my opinion, the speed issue is with the server. The server may be erratic in its response times. Nothing you can do about it on the front end of it.


i have used this
B4X:
Dim j As HttpJob
            j.Initialize("", Me)
            Dim link As String = "http://kccug.com/KabojjaApp/RecieveSMS.ashx"
            Dim text As String = $"customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}"$
            j.PostString(link, text)
    
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.GetString)
            End If
It is now asking for the parameter values


B4X:
1590681075798
28/05/2020 06:51:15 pm
1590681075798
28/05/2020 06:51:15 pm
28/05/2020 06:51:15 pm
ResponseError. Reason: Internal Server Error, Response: <!DOCTYPE html>
<html>
    <head>
        <title>Procedure or function 'Recievesms' expects parameter '@c', which was not supplied.</title>
        <meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
         @media screen and (max-width: 639px) {
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
         }
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Looks like the service you are using is expecting the parameters in the URL. So you're back to the code you posted in your first post. Except, I would use Download2 instead of Download
B4X:
Dim j As HttpJob
            j.Initialize("", Me)
            Dim link As String = "http://kccug.com/KabojjaApp/RecieveSMS.ashx"
            Dim parameters() as String = Array As String("customerId" , act, "s", edtMessage.Text, "d", getdate(DateTime.Now), "id", NewID, "ph", phone, "f", sx)
            j.Download2(link, parameters)
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.GetString)
            End If
Note 1: Not tested, may contain typos

code below is sometimes it is slow
I'm pretty sure it has nothing to do with the code, but rather
1) The network between you and the server
or
2) The server responding faster or slower at any given time
or
3) Something else on the device hogging up CPU
or
4) Something else in your app hogging up CPU
or
5) Some various combination of the above
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Looks like the service you are using is expecting the parameters in the URL. So you're back to the code you posted in your first post. Except, I would use Download2 instead of Download
B4X:
Dim j As HttpJob
            j.Initialize("", Me)
            Dim link As String = "http://kccug.com/KabojjaApp/RecieveSMS.ashx"
            Dim parameters() as String = Array As String("customerId" , act, "s", edtMessage.Text, "d", getdate(DateTime.Now), "id", NewID, "ph", phone, "f", sx)
            j.Download2(link, parameters)
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.GetString)
            End If
Note 1: Not tested, may contain typos


I'm pretty sure it has nothing to do with the code, but rather
1) The network between you and the server
or
2) The server responding faster or slower at any given time
or
3) Something else on the device hogging up CPU
or
4) Something else in your app hogging up CPU
or
5) Some various combination of the above

Thank you it has finally worked
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
'Recievesms' expects parameter '@c', which was not supplied.


I don't have much experience in this area , but after reading above replies , I ran a test... ( directly from web browser)

Firstly I included the missing '@c' parameter. (using my email address)
B4X:
http://kccug.com/KabojjaApp/RecieveSMS.ashx?@c=myemailxxxxxx&customerId=${act}&s=${edtMessage.Text}&d=${getdate(DateTime.Now)}&id=${NewID}&ph=${phone}&f=${sx}

The result/response was ... String was not recognized as a valid DateTime.

So I tried ...

B4X:
http://kccug.com/KabojjaApp/RecieveSMS.ashx?@c=myemailxxxxxx&customerId=${act}&s=${edtMessage.Text}&d=2020/05/05&id=${NewID}&ph=${phone}&f=${sx}

The result/response was ... False


Are you meant to include an email parameter in Posted data ? (or is @c a parameter for some other piece of data)

Unsure if this helps in anyway.
 
Upvote 0
Top