Android Tutorial HttpUtils2 - Web services are now even simpler

miguelm

New Member
Licensed User
Longtime User
Hi Erel and Everybody.

How may I to accessing from B4A to a web service method that resides in a web server?

Example: I am executing in my PC a web service (myService.asmx), and I need to access a particular method of myService.asmx from another application in B4A.

Something like ... CallSub("myService", "myServiceMethod")

Thank you.

Please forgive my english.
 
Last edited:

aminoacid

Active Member
Licensed User
Longtime User

how do you specify the URL
 

boten

Active Member
Licensed User
Longtime User
Trying to do all download HttpJob in a code module

This works: (JobDone sub in Main)
B4X:
Dim Job As HttpJob
Job.Initialize("jobname","main")
Job.Download(url)

But trying to process the result inside the code module does NOT work (JobDone sub in code module)
B4X:
Dim Job As HttpJob
Job.Initialize("jobname","<code-modle-name>")
Job.Download(url)

Is there a way to process the JobDone inside the code module?

unfiltered log is:
B4X:
** Service (httputils2service) Create **
** Service (httputils2service) Start **
GC_FOR_MALLOC freed 3418 objects / 199504 bytes in 43ms
Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x4001d800)
java.lang.RuntimeException: java.lang.NoSuchFieldException: processBA
   at anywheresoftware.b4a.keywords.Common$5.run(Common.java:960)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: processBA
   at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
   at java.lang.Class.getField(Class.java:881)
   at anywheresoftware.b4a.keywords.Common.getComponentBA(Common.java:997)
   at anywheresoftware.b4a.keywords.Common.access$0(Common.java:986)
   at anywheresoftware.b4a.keywords.Common$5.run(Common.java:905)
   ... 9 more
FATAL EXCEPTION: main
java.lang.RuntimeException: java.lang.NoSuchFieldException: processBA
   at anywheresoftware.b4a.keywords.Common$5.run(Common.java:960)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: processBA
   at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
   at java.lang.Class.getField(Class.java:881)
   at anywheresoftware.b4a.keywords.Common.getComponentBA(Common.java:997)
   at anywheresoftware.b4a.keywords.Common.access$0(Common.java:986)
   at anywheresoftware.b4a.keywords.Common$5.run(Common.java:905)
   ... 9 more
  Force finishing activity ...............
 

boten

Active Member
Licensed User
Longtime User
... You have to process JobDone in the same activity that you initialize the Job in.
The code snippets I posted above ARE from the code module. In effect the "working" code calls JobDone in Main. My question still stands
 

boten

Active Member
Licensed User
Longtime User
I'm not making any assumption. I'm stating the facts:
this code :
B4X:
Dim Job As HttpJob
Job.Initialize("jobname","main")
Job.Download(url)

is in the code module

the JobDone sub is in Main

This "combination" works OK

obviously the initialize/download is in one module (code) and the JobDone is in another module (Main).
Still, I'd like to know why the JobDone can not be in code module too?
 

boten

Active Member
Licensed User
Longtime User
I think I got it (Erel can verify this):
Since JobDone is the HttpJob's event and code modules can NOT handle events - hence the problem.
 

Ramanathan Subbiah

New Member
Licensed User
Longtime User
Hi Erel,
Can you post a sample code which would demonstrate how to connect to a https server, accept the certificates and retrieve the contents. I'm quite confused with HttpUtils, HttpUtils2, Httpjob etc. Is there a sequential way to learn about https and http connections.
 

Ramanathan Subbiah

New Member
Licensed User
Longtime User
HttpUtils is the old version. It is not relevant any more.

Certificates are handled automatically. You don't need to do anything special. Just follow the tutorial in the first post.

Thanks for the reply but, I'm trying to connect to my localhost with an untrusted certificate. I get this error:
Error: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
I used the steps as in your first post.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The correct behavior is to reject unknown certificates unless you (the developer) explicitly configure it to accept all certificates.

Follow these steps:
- Uncheck HttpUtils2 library
- Add HttpUtils2 modules (from the first post)
- Check StringUtils and Http libraries
- Change hc.Initialize in HttpUtils2Service to hc.InitializeAcceptAll
 

Douglas Farias

Expert
Licensed User
Longtime User
erel its possible send a image to host with this lib?
 

Douglas Farias

Expert
Licensed User
Longtime User
can you show me a simple exemple?
to send a image to vps?

to get file your code works fine
B4X:
  'Send a GET request
  job3.Initialize("Job3", Me)
  job3.Download("http://www.b4x.com/forum/images/categories/android.png")

but to send how can i ?

can you show me a simple code to send ?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…