新手报到,请教Web Services问题。

taihongbo

New Member
如何访问 Web Services ?


没有人回答,还是我自己解决了,分享一下。http://www.saas56.info

Basic4android 访问 Web.Services 问题解决
1、开发环境 :
vs2008
Basic4android
2、vs2008 的 Web.Services

以下代码 vs2008 创建 Services 项目时产生
我把 HelloWorld 做了一个可以传递参数的过程。


Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。http://taihongbo.28.xindns.org/
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function HelloWorld(ByVal cName As String) As String
Return "你好!" & cName
End Function
End Class


----下面的很重要,原来出错的原因(Basic4android不能访问Web.Services)
在 <system.web> 节添加:

<webServices>
<protocols>
<add name="HttpPost" />
<add name="HttpGet" />
<add name="HttpSoap" />
<add name="Documentation" />
</protocols>
</webServices>

</system.web>


3、Basic4android 代码



'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
.
Dim URL As String
URL = "http://taihongbo.28.xindns.org/Service1.asmx" '我的Service地址 远程)
'URL = "http://192.168.1.100:8080/Service1.asmx" '(本地)

Dim HttpClient1 As HttpClient

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Log("************************")
HttpClient1.Initialize("HttpClient1")

Dim request As HttpRequest
request.InitializeGet(URL & "/HelloWorld?cName=Basic4android") '传递参数
request.Timeout = 10000 '超时
If HttpClient1.Execute(request, 1) = False Then Return
ProgressDialogShow("正在链接Service...")

End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log("ResponseSuccess")
ProgressDialogHide
Dim result As String
result = Response.GetString("UTF8") '
Log(result)
Dim rate As String

i = result.IndexOf(".org/") '见 vs2008 中 "http://tempuri.org/")> _
i2 = result.IndexOf2("<", i + 1)
rate = result.substring2(i + 7, i2)
Log("Rate = " & rate)
Msgbox( rate,"结果")
End Sub
Sub HttpClient1_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
Log(Reason)
Log(StatusCode)
ProgressDialogHide
msg = "Error connecting to server."
If reason <> Null Then msg = msg & CRLF & Reason
ToastMessageShow (msg, True)
End Sub
 
Last edited:
Top