Android Question Getting data from an ASP.NET Web Service using HttpUtils2 with OkHttpClient

GlenAlan

Member
Licensed User
Longtime User
I have a working b4a app that retrieves data from an ASP.Net web service, but it uses the older HttpUtils method. I want to upgrade the code to rather use the newer HttpUtils2 method (with OkHttpClient), but it just refuses to work.

I based my small test program on your HttpUtils2 demo as below, but this keeps failing with the error: Internal Server Error. And yet the app using the older HttpUtils method retrieves the data just fine.

I assume the error is coming from the Web Service, but I don't know what problem it is encountering.

Please help.

B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: HttpUtils2http:Test a Web Service
   #VersionCode: 1
   #VersionName:
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
   #LibraryVersion: 2.10
   #LibraryName: HttpUtils2 (OkHttpClient)
#End Region

'Activity module

Sub Activity_Create(FirstTime As Boolean)
  Dim job1 As HttpJob
  Dim XML As String
  XML = ""
  XML = XML & "<?xml version='1.0' encoding='utf-8'?>"
  XML = XML & "<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>"
  XML = XML & "<soap12:Body>"
  XML = XML & "<JSONService xmlns='http://gapsoftware.co.za/'>"
  XML = XML & "<SearchTable>28b946e6c826b502|MP000001|77|stock</SearchTable>"
  XML = XML & "<SQLQuery>select top 5 * from stock order by [code]</SQLQuery>"
  XML = XML & "</JSONService>"
  XML = XML & "</soap12:Body>"
  XML = XML & "</soap12:Envelope>"
  XML = XML.Replace("'", Chr(34))  
  Log("XML >>>>: " & XML)
  job1.Initialize("Job1", Me)
  job1.PostString("http://cloud.omegaonline.co.za:88/RevService1.asmx",XML)
  job1.GetRequest.SetContentType("application/soap+xml")
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
     Log(Job.GetString) 
   Else
     Log("Error: " & Job.ErrorMessage)
     ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub
 
Last edited:

GlenAlan

Member
Licensed User
Longtime User
Note that you can use the smart strings literal: https://www.b4x.com/android/forum/threads/50135/#content

Can you post the code that worked with HttpUtils?

Hi Erel

Thanks for your assistance. Sorry for the delay - the old code is embedded across multiple modules so I had to cobble together a small program to do a single web service call using the old HttpUtils code that preceded HttpUtils2. However, I notice that it does use the HttpUtils2 library, but uses subroutines in a module named XMLUtils that I downloaded from your site 2 years ago.

I've attached a zip of that project. It successfully retrieves data from the ASP.NET web service (in the form of a JSON string). I'd prefer to use the newer mechanisms laid out in your new HttpUtils2 demo (that uses the OkHttp lib instead of Http)

PS: There seems to be a problem with parsing JSON strings in Android 5+. My app parses the JSON string into a List (using parser.NextArray). On Android 4- it works fine, but on Android 5+ apparently nothing gets parsed. I don't have a device with Android 5, but those clients of mine using Android 5+ all report a blank screen when a query returns more than 1 record in the JSON string? Are you aware of a problem with the JSON library?
 

Attachments

  • HttpUtilsOld.zip
    9.5 KB · Views: 167
Upvote 0

GlenAlan

Member
Licensed User
Longtime User
Sorry, Erel. The POST payloads were different because, in desperation, I also tried the SOAP 1.2 format, but both formats resulted in the same error.

I've attached both the OLD program (that works) as well as the NEW program (that uses OkHttp) that now returns ERROR: UNSUPPORTED MEDIA TYPE. Is it not something to do with the SetContentType("application/xml+soap") instruction? I've tried a number of different instructions, but they all fail.

The POST Payloads in both programs are identical as below:
B4X:
XMLNew >>>>:
<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
     <soap:Body>
      <JSONService xmlns="http://GapSoftware.co.za/">
        <SearchTable>28b946e6c826b502|MP000001|77|stock
        </SearchTable>
        <SQLQuery>select top 2 [Code],Descrip,InclPrice,OnHand from stock where left([code],1)='a' order by [code]
        </SQLQuery>
      </JSONService>
    </soap:Body>
  </soap:Envelope>


XMLOld >>>>:
<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
  <JSONService xmlns="http://GapSoftware.co.za/">
  <SearchTable>28b946e6c826b502|MP000001|77|stock
  </SearchTable>
  <SQLQuery>select top 2 [Code],Descrip,InclPrice,OnHand from stock where left([code],1)='a' order by [Code]
  </SQLQuery>
  </JSONService>
  </soap:Body>
</soap:Envelope>
 

Attachments

  • HttpUtilsOld.zip
    9.6 KB · Views: 159
  • HttpUtilsNew.zip
    9 KB · Views: 218
Upvote 0

GlenAlan

Member
Licensed User
Longtime User
Thank you, Erel. That works!

I've just been reading your tutorial on smart string literals and I will be converting my code accordingly. Thanks for the tip.

Is there a list of choices or a discussion over GetRequest.SetContentType somewhere that would prevent wasting time in future?
 
Upvote 0

GlenAlan

Member
Licensed User
Longtime User
I was searching your B4x site for SetContentType instead of a wider Google search. My apologies.

Thanks again.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…