Android Question Consuming .Net webservices

Theo_van_Putten

New Member
Licensed User
Longtime User
I am trying to use Webservices.

The first thing I have done is to modify the CurrencyConverter example in such a way that it calls my webservice. My webservice is extremely simple. I has a method called HelloWorld. And this method simply returns a string with the text "hello world". Exciting. Indeed.

But I can not get it to work.

Statements:

Sub HttpClient1_ResponseSuccess (Response AsHttpResponse, TaskId AsInt)
Log("ResponseSuccess")
Log(Response)
ProgressDialogHide
Dim Result AsString
Dim ResultLength AsDouble
ResultLength = Response.ContentLength
Result = Response.GetString("UTF8") 'Convert the response to a string

And on the last line it stops. with:

http://192.168.2.109:12749/WS2/Service1.asmx?op=HelloWorld
ResponseSuccess
anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper@426a4408
main_httpclient1_responsesuccess (B4A line: 126)
Result = Response.GetString("UTF8") 'Convert the response to a string
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:121)
at org.apache.http.conn.BasicManagedEntity.streamClosed(BasicManagedEntity.java:179)
at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:266)
at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:213)
at java.io.InputStreamReader.close(InputStreamReader.java:145)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:139)
at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.GetString(HttpClientWrapper.java:489)
at b4a.converter.main._httpclient1_responsesuccess(main.java:698)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA$3.run(BA.java:312)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5414)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
android.os.NetworkOnMainThreadException
Just before I try to convert the response into a string, I have asked for the length of the response.
It is about 5721 charaters. While the length of the response from teh osiginal CurrencyConverter Webservice was about 97. ????

If I try to invoke my webservice on the server I get as response:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="Hello'>http://tempuri.org/">Hello World</string>

So, I think the webservice is OK.

Why can i not convert the returned respone into a string? Why is it so long?

Any help would be much appreciated.
 

Theo_van_Putten

New Member
Licensed User
Longtime User
Okay. I will usHttp[Utils2.

As follows:

Sub Activity_Create(FirstTime As Boolean)
Dim job1, job2, job3 As HttpJob
'Send a POST request
job1.Initialize("Job1", Me)
job1.PostString("http://192.168.2.109:12749/WS2/Service1.asmx?op=HelloWorld", "")
End Sub
Sub JobDone (Job As HttpJob)
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "Job1"
'print the result to the logs
Log(Job.GetString)

End Select
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub

And the reult is:

PackageAdded: package:b4a.example


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


startService: class anywheresoftware.b4a.samples.httputils2.httputils2service


** Service (httputils2service) Create **


** Service (httputils2service) Start **


** Activity (main) Resume **


<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Root element is missing.

at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)

--- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
JobName = Job1, Success = false
Error: Internal Server Error
** Activity (main) Pause, UserClosed = true **

But when I test the Webservice with Webservice Studio, there are no problems.

????????
 
Upvote 0
Top