从Android调用Web服务时出现异常

时间:2017-04-06 10:36:42

标签: java android asp.net web-services

我是初学者,我在从android调用asp.net web服务方法(SignUp)时面临一个问题。当我从浏览器尝试它时,它工作正常,但当我从Android做它时,它给了我一些例外 "

  

SoapFault - faultcode:' soap:Client' faultstring:' System.Web.Services.Protocols.SoapException:服务器无法识别HTTP标头SOAPAction的值:SignUp。      在System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()      在System.Web.Services.Protocols.SoapServerProtocol.Initialize()      在System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型,HttpContext上下文,HttpRequest请求,HttpResponse响应)      在System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,HttpContext context,HttpRequest request,HttpResponse response,Boolean& abortProcessing)' faultactor:' null'细节:org.kxml2.kdom。"

这是我的Java注册类

HandlerStack

这是我的Java WebService类

String SOAP_ACTION1 = "http://tempuri.org/SignUp";
String METHOD_NAME1 = "SignUp";
btn = (Button)findViewById(R.id.btn_signup);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new WebService().execute(METHOD_NAME1,SOAP_ACTION1,txt_name.getText().toString(),                  txt_email.getText().toString(),txt_password.getText().toString(),
                    txt_address.getText().toString(),
                    txt_dob.getText().toString(),
                    txt_account.getText().toString(),                       
                 txt_cnic.getText().toString(),txt_pobox.getText().toString(),                       
               txt_city.getText().toString(),txt_status.getText().toString());

这是我的asp.net WebService方法

 private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://192.168.0.102/MyWebService/WebService1.asmx?WSDL";
 protected String doInBackground(String... params) {
    String s="";
    String whichmethodcall = params[0];
    switch(whichmethodcall){
        case "SignUp":
            s = signup(params);
            break;
        case "SignIn":
            s = signin(params);
            break;
    }
    return s;
}
 public String signup(String[] arr){
    String s = "";
    SoapObject request = new SoapObject(NAMESPACE, arr[1]);
    //Use this to add parameters
    request.addProperty("cname",arr[2]);
    request.addProperty("cemail",arr[3]);
    request.addProperty("cpwd",arr[4]);
    request.addProperty("caddress",arr[5]);
    request.addProperty("cdob",arr[6]);
    request.addProperty("caccount",arr[7]);
    request.addProperty("cCnic",arr[8]);
    request.addProperty("cpobox",arr[9]);
    request.addProperty("cCity",arr[10]);
    request.addProperty("cstatus",arr[11]);

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new 
    SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //this is the actual part that will call the webservice
        androidHttpTransport.call(arr[0], envelope);

        // Get the SoapResult from the envelope body.
       // SoapObject result = (SoapObject)envelope.bodyIn;
       // SoapObject result = (SoapObject) envelope.getResponse();
        if (envelope.bodyIn instanceof SoapFault) {
            final SoapFault result = (SoapFault) envelope.bodyIn;
            if (result != null) {
                //Get the first property and change the label text
                   s = result.toString();
            } else
                s = "No response";              
        }
    } catch (Exception e) {
        e.printStackTrace();
        s=e.getMessage().toString();
    }
    return s;
}

1 个答案:

答案 0 :(得分:0)

你能试试吗

代替SoapObject request = new SoapObject(NAMESPACE,arr [1]); 使用

SoapObject request = new SoapObject(NAMESPACE,arr [0]);

在soap对象中,您应该只传递方法名称,而不使用http:// ...