【发布时间】:2015-07-02 19:53:13
【问题描述】:
我已经使用 ASP.NET 创建了一个 Web 服务,然后我在使用 android 应用程序连接到它时遇到了问题。 我的代码中的方法 Hello world 可以正常工作,因为它没有参数,但方法 add 没有。
//ASP.NET 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://ahmadezzat.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
SqlConnection connection = new SqlConnection();
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int HelloWorld()
{
return 5;
}
[WebMethod]
public int echo(int x) {
return x;
}
}
安卓代码
package com.me;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SignUp extends Activity {
private static final String SOAP_ACTION = "http://ahmadezzat.com/echo";
private static final String METHOD_NAME = "echo";
private static final String NAMESPACE = "ServiceReference1";
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
Button button = (Button) findViewById(R.id.signUp);
final TextView tv = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String result = callWebService(22);
tv.setText("the result is " + result);
}
});
}
public String callWebService(int x) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty("msg", "1");
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(x);
pi.setType(int.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(urll);
androidHttpTransport.debug = true;
//return androidHttpTransport.requestDump + " / " + androidHttpTransport.responseDump;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive res = (SoapPrimitive) envelope.getResponse();
String v = res.toString() + "\n";
v += androidHttpTransport.requestDump + "\n" + androidHttpTransport.responseDump;
return v;
} catch (Exception E) {
return E.toString();
}
}
}
跟踪请求后是这样的
<v:Body>
<echo xmlns="ServiceReference1" id="o0" c:root="1">
<x i:type="d:int">22</x>
<echo>
<v:/Body>
响应是
<Soap:Body>
<echoResponse xmlns="http://www.ahmadezzat.com"/>
<echoResult>0</echoResult>
<echoResponse>
<Soap:/Body>
【问题讨论】:
标签: android asp.net web-services ksoap2