Android 连接 Asp.net 网络服务参数
Android connecting Asp.net web service parameters
我使用 ASP.NET 创建了一个网络服务,然后我在使用 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;
}
}
Android代码
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>
尝试这样的事情:
替换您的以下代码:
PropertyInfo pi = new PropertyInfo();
pi.setName("msg");
pi.setValue(x);
pi.setType(int.class);
request.addProperty(pi);
和
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(x);
pi.setType(String.class);
request.addProperty(pi);
因为 "x" 定义您正在为网络服务中的参数 "x" 发送值
我一般都是这样用的,都经过测试干净了:
如果网络结果是一个对象 CallServerObject.java 像 string 或 int:
import java.util.List;
import org.apache.http.NameValuePair;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.ebrahimi.helper.Constants;
public class CallServerObject {
private static String PAGE_NAME;
public static void setPAGE_NAME(String pageName) {
PAGE_NAME = pageName;
}
private static String METHOD_NAME;
public static void setMETHOD_NAME(String methodName) {
METHOD_NAME = methodName;
}
private static List<NameValuePair> PARAMS;
public static void setPARAMS(List<NameValuePair> params) {
PARAMS = params;
}
public Object GetObject() {
SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
for (NameValuePair prop : PARAMS) {
request.addProperty(prop.getName(), prop.getValue());
}
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
Object javab = null;
try {
androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
javab = (Object)envelope.getResponse();
} catch (Exception e) {
javab = e.getLocalizedMessage();
}
return javab;
}
private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
ht.debug = true;
ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");
return ht;
}
}
如果服务器 return SoapObject CallServerSopaObject 喜欢我使用的列表:
import java.util.List;
import org.apache.http.NameValuePair;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.ebrahimi.helper.Constants;
public class CallServerSopaObject {
private static String PAGE_NAME;
public static void setPAGE_NAME(String pageName) {
PAGE_NAME = pageName;
}
private static String METHOD_NAME;
public static void setMETHOD_NAME(String methodName) {
METHOD_NAME = methodName;
}
private static List<NameValuePair> PARAMS;
public static void setPARAMS(List<NameValuePair> params) {
PARAMS = params;
}
public SoapObject GetObject() {
SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
for (NameValuePair prop : PARAMS) {
request.addProperty(prop.getName(), prop.getValue());
}
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
SoapObject javab = null;
try {
androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
javab = (SoapObject)envelope.getResponse();
} catch (Exception e) {
e.getLocalizedMessage();
}
return javab;
}
private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
ht.debug = true;
ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");
return ht;
}
}
现在当我需要连接到服务器时,对于每个 asp.net 函数 CheckCode.java 我有这个代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.ebrahimi.list.BasketListView;
import com.ebrahimi.server.CallServerObject;
import com.ebrahimi.server.Server;
import com.ebrahimi.server.Shoper;
import com.ebrahimi.shiriniyazd.R;
import android.app.ProgressDialog;
import android.os.AsyncTask;
public class CheckCode {
private ProgressDialog pDialog;
private Object javab = null;
private String code;
private String cell;
public BasketListView context;
public CheckCode (BasketListView act,
String code,
String cell) {
this.context = act;
this.code = code;
this.cell = cell;
}
public void callCheckRegister(){
new callDics().execute();
}
private class callDics extends AsyncTask<String, String, Object > {
@Override
protected Object doInBackground(String... params) {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
CallServerObject list = new CallServerObject();
CallServerObject.setPAGE_NAME( Server.PageCommons.PageNameShoper);
CallServerObject.setMETHOD_NAME(Shoper.Methods.MethodCheckUserByGUID);
params1.add(new BasicNameValuePair("userName", cell));
params1.add(new BasicNameValuePair("code" , code));
params1.add(new BasicNameValuePair("getImage", "true"));
CallServerObject.setPARAMS(params1);
publishProgress(context.getString(R.string.msg_wait_connecting));
javab = list.GetObject();
return javab;
}
@Override
protected void onProgressUpdate(String... progress) {
super.onProgressUpdate(progress);
if (pDialog != null) {
pDialog.setMessage(progress[0]);
}
}
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(context);
pDialog.setMessage(context.getString(R.string.msg_wait));
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onPostExecute(Object javab) {
pDialog.dismiss();
if (isCancelled()) {
}
}
}
}
现在我已经在一个地方收集了所有名称和 url,并像这样定义它们:
public static String MAIN_URL = "http://ahmadezzat.com/";
public static final String PageName = "myPaageName.asmx";
public static String MethodCheckUserByGUID = "helloWorldMethod";
按此方式查看,如果有任何问题,请告诉我,希望对您有所帮助。
你的代码只有一个问题:
使用此命名空间“http://ahmadezzat.com/”而不是 "ServiceReference1"
private static final String NAMESPACE = "http://ahmadezzat.com/";
如果您尝试在 android 版本 4.* 上测试 android 代码,它会出现异常 "NetworkOnMainThreadException" 并且无法正常工作。
所以你需要使用 AsyncTask 来避免这种情况。
developer.android.com/reference/android/os/AsyncTask.html
Android 编辑后的代码:
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.AsyncTask;
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 = "http://ahmadezzat.com/";
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";
private TextView tv;
/** 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);
tv = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CallWebService().execute(22);
}
});
}
class CallWebService extends AsyncTask<Integer, Void, String> {
@Override
protected String doInBackground(Integer... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(params[0]);
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;
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();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(result);
}
}
}
我使用 ASP.NET 创建了一个网络服务,然后我在使用 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;
}
}
Android代码
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>
尝试这样的事情:
替换您的以下代码:
PropertyInfo pi = new PropertyInfo();
pi.setName("msg");
pi.setValue(x);
pi.setType(int.class);
request.addProperty(pi);
和
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(x);
pi.setType(String.class);
request.addProperty(pi);
因为 "x" 定义您正在为网络服务中的参数 "x" 发送值
我一般都是这样用的,都经过测试干净了: 如果网络结果是一个对象 CallServerObject.java 像 string 或 int:
import java.util.List;
import org.apache.http.NameValuePair;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.ebrahimi.helper.Constants;
public class CallServerObject {
private static String PAGE_NAME;
public static void setPAGE_NAME(String pageName) {
PAGE_NAME = pageName;
}
private static String METHOD_NAME;
public static void setMETHOD_NAME(String methodName) {
METHOD_NAME = methodName;
}
private static List<NameValuePair> PARAMS;
public static void setPARAMS(List<NameValuePair> params) {
PARAMS = params;
}
public Object GetObject() {
SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
for (NameValuePair prop : PARAMS) {
request.addProperty(prop.getName(), prop.getValue());
}
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
Object javab = null;
try {
androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
javab = (Object)envelope.getResponse();
} catch (Exception e) {
javab = e.getLocalizedMessage();
}
return javab;
}
private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
ht.debug = true;
ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");
return ht;
}
}
如果服务器 return SoapObject CallServerSopaObject 喜欢我使用的列表:
import java.util.List;
import org.apache.http.NameValuePair;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.ebrahimi.helper.Constants;
public class CallServerSopaObject {
private static String PAGE_NAME;
public static void setPAGE_NAME(String pageName) {
PAGE_NAME = pageName;
}
private static String METHOD_NAME;
public static void setMETHOD_NAME(String methodName) {
METHOD_NAME = methodName;
}
private static List<NameValuePair> PARAMS;
public static void setPARAMS(List<NameValuePair> params) {
PARAMS = params;
}
public SoapObject GetObject() {
SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
for (NameValuePair prop : PARAMS) {
request.addProperty(prop.getName(), prop.getValue());
}
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
SoapObject javab = null;
try {
androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
javab = (SoapObject)envelope.getResponse();
} catch (Exception e) {
e.getLocalizedMessage();
}
return javab;
}
private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
ht.debug = true;
ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");
return ht;
}
}
现在当我需要连接到服务器时,对于每个 asp.net 函数 CheckCode.java 我有这个代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.ebrahimi.list.BasketListView;
import com.ebrahimi.server.CallServerObject;
import com.ebrahimi.server.Server;
import com.ebrahimi.server.Shoper;
import com.ebrahimi.shiriniyazd.R;
import android.app.ProgressDialog;
import android.os.AsyncTask;
public class CheckCode {
private ProgressDialog pDialog;
private Object javab = null;
private String code;
private String cell;
public BasketListView context;
public CheckCode (BasketListView act,
String code,
String cell) {
this.context = act;
this.code = code;
this.cell = cell;
}
public void callCheckRegister(){
new callDics().execute();
}
private class callDics extends AsyncTask<String, String, Object > {
@Override
protected Object doInBackground(String... params) {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
CallServerObject list = new CallServerObject();
CallServerObject.setPAGE_NAME( Server.PageCommons.PageNameShoper);
CallServerObject.setMETHOD_NAME(Shoper.Methods.MethodCheckUserByGUID);
params1.add(new BasicNameValuePair("userName", cell));
params1.add(new BasicNameValuePair("code" , code));
params1.add(new BasicNameValuePair("getImage", "true"));
CallServerObject.setPARAMS(params1);
publishProgress(context.getString(R.string.msg_wait_connecting));
javab = list.GetObject();
return javab;
}
@Override
protected void onProgressUpdate(String... progress) {
super.onProgressUpdate(progress);
if (pDialog != null) {
pDialog.setMessage(progress[0]);
}
}
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(context);
pDialog.setMessage(context.getString(R.string.msg_wait));
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onPostExecute(Object javab) {
pDialog.dismiss();
if (isCancelled()) {
}
}
}
}
现在我已经在一个地方收集了所有名称和 url,并像这样定义它们:
public static String MAIN_URL = "http://ahmadezzat.com/";
public static final String PageName = "myPaageName.asmx";
public static String MethodCheckUserByGUID = "helloWorldMethod";
按此方式查看,如果有任何问题,请告诉我,希望对您有所帮助。
你的代码只有一个问题: 使用此命名空间“http://ahmadezzat.com/”而不是 "ServiceReference1"
private static final String NAMESPACE = "http://ahmadezzat.com/";
如果您尝试在 android 版本 4.* 上测试 android 代码,它会出现异常 "NetworkOnMainThreadException" 并且无法正常工作。 所以你需要使用 AsyncTask 来避免这种情况。
developer.android.com/reference/android/os/AsyncTask.html
Android 编辑后的代码:
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.AsyncTask;
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 = "http://ahmadezzat.com/";
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";
private TextView tv;
/** 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);
tv = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CallWebService().execute(22);
}
});
}
class CallWebService extends AsyncTask<Integer, Void, String> {
@Override
protected String doInBackground(Integer... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(params[0]);
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;
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();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(result);
}
}
}