我正在尝试使用 KSOAP2 库从 Android 应用程序调用本地 Web 服务 ASP.net (C#),但是当我从 Web 服务调用 WebMethod 时,出现以下异常:
这是主要的活动代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.AlteredCharSequence;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;;
public class MainActivity extends Activity {
public static String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView TT = (TextView) findViewById(R.id.txt);
try
{
result = "START";
Caller CC = new Caller();
CC.join();
CC.start();
while (result == "START")
{
try
{
Thread.sleep(10);
}
catch (Exception s){}
}
TT.setText(result);
}
catch (Exception e)
{
TT.setText(e.toString());
}
}
});
}
}
这是调用者类:
public class Caller extends Thread {
CallSoap SS;
public void run (){
try
{
SS = new CallSoap();
MainActivity.result = SS.call();
}
catch (Exception ex)
{
MainActivity.result = ex.toString();
}
}
}
最后是 CallSoap 类:
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 org.ksoap2.serialization.PropertyInfo;
public class CallSoap {
public final String SOAP_ACTION = "http://tempuri.org/Add";
public final String OPERATION_NAME = "Add";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "http://10.0.2.2:9377/Service.asmx";
public CallSoap () {
}
public String call ()
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
request.addProperty("a",100);
request.addProperty("b",200);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
SoapPrimitive response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = (SoapPrimitive) envelope.getResponse();
}
catch (Exception exception)
{
return exception.toString();
}
return response.toString();
}
}
而从web service调用web方法很简单,取两个整数并返回它们之和,方法名是Add
,第一个参数是a
,第二个是b
任何人都可以帮助解决这个异常吗?:)