B4J Question How to return HTTP request result in web service

Fifi Donkor

Member
Licensed User
Longtime User
I have made a http web service application which will be called from remote clients (browser).
This web service also in turn fetches data from another web service running locally on the server via http ReST calls, then is supposed to return what it got from the local service to the remote client. In short, my application, an intermediary server, is also a client to that other local server.

I have had a problem figuring out how to get the response from the local service into my handler that is supposed to return the result to the remote clients, since the call to the local other server is asynchronous and so does not return anything directly. In other words how do I get my "Sub Handle(req As ServletRequest, resp As ServletResponse)" to resp.Write(the result of my http client)?

I guess there's probably a simple way to do this that I'm just not getting.
Help appreciated.
 

Fifi Donkor

Member
Licensed User
Longtime User
Thanks, somed3v3loper and Erel, but somehow this solution has not worked for me yet. I have tried many different iterations of little modifications to the code in the referenced link above without success.

There is one fundamental difference in what I'm trying to do. In my case I'm not downloading a file from the other service, but rather it returns a string (JSON from a local OrientDB API service) that I need to pass back to the initial caller of my service. I initially got a 500 error with the following reason:

"java.lang.RuntimeException: java.lang.IllegalArgumentException: unexpected url: localhost:2480/function/steel/somrandom"

So I changed the j.Download(...) line to j.GetString2("localhost:2480/function/steel/somrandom") and got an error:

"java.lang.RuntimeException: java.io.FileNotFoundException: C:\Users\User\AppData\Local\Temp (Access is denied)"

Then I changed File.Copy2(Job.GetInputStream, mResp.OutputStream) line in Job.Success condition to Job.GetString2("localhost:2480/function/steel/somrandom") and still got the last error, which seemed strange because I wasn't trying to access any file anywhere. I went ahead to comment all the code in the Job.Succcess condition, and still got that error, which suggested to me that the problem was with the first line I changed ( the j.Download(...) line ).

I have tweaked the code in other little ways and still can't get it to work.
 
Upvote 0

Fifi Donkor

Member
Licensed User
Longtime User
Still thinking...
Something like resp.Write(the result of my OrientDB call) would have worked perfectly, except that since it's asynchronous I cant access that result in the Sub handle, so I guess that approach may be wrong.
I don't thoroughly understand the way to use this StartMessageLoop/EndMessageLoop so I'm wondering if that is why I'm having this problem.
Can someone post a version of the code snippet referenced by somed3v3loper that works with receiving and passing on a string, and not a file?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Fifi Donkor

Member
Licensed User
Longtime User
After two painstaking days of trial and error, I have finally found why my application was not working.

Erel, I do not know if the HttpJob object specification has changed since that post https://www.b4x.com/android/forum/threads/creating-a-proxy.63292/#post-400026 or it is because I am using a different library (OkHttpUtils2_NONUI version 2.3) as I think is recommended for my scenario. In your code snippet the Job was initialized with the JobName as the second parameter instead of the first. I realized that when I "corrected" my code by flipping the parameters around, that is passing the name first then the target module, it all worked.

That post may need to be corrected for the sake of the record, and others' reference.

Thanks for the help.
 
Upvote 0
Top