2013-03-05 109 views
0

我正在编写一个Android应用程序,使用KSOAP与Web服务进行通信。 Web服务和Android应用程序之间的连接正在工作,因为我可以调用web服务并获取返回值(hello)。但是,如果我尝试通过.addProperty将应用程序名称提供给Web服务,则Web服务将返回一个空对象。从Android应用程序调用Web服务返回空对象

这里是我的代码:

MainActivity:

private final String NAMESPACE_Local = "http://test.com/"; 
    private final String URL_Local = "http://168.185.226.21:7001/myTest/myTestWebServiceService"; 
    private final String SOAP_ACTION_Local = "Hello_Action_Extend"; 
    private final String METHOD_NAME_Local = "hello_extend"; 

    public void LocalServer(View view) 
    { 
     TextView text = (TextView) findViewById(R.id.update_text); 

     SoapObject request = new SoapObject(NAMESPACE_Local, METHOD_NAME_Local); 
     request.addProperty("name", "Christian"); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Local); 

     try { 
      androidHttpTransport.call(SOAP_ACTION_Local, envelope); 
      SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
      Log.i("myApp", response.toString()); 


      text.setText(response.toString()); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(this,"Device or service offline",Toast.LENGTH_LONG).show(); 
     } 
    } 

的WebServer:

package com.test; 

import javax.jws.*; 

@WebService 
public class myTestWebService { 

    @WebMethod(action="Hello_Action") //that method works 
    public String hello() { 
     return "hello"; 
    } 

    @WebMethod(action="Hello_Action_Extend") 
    public String hello_extend(String name) //that works also, but it is giving back "hello null" 
    { 
     return "hello "+name; 
    } 
} 

我希望你能帮帮我!

回答

1

尝试更换:

request.addProperty("name", "Christian"); 

为:

request.addProperty("name",ElementType.STRING_CLASS, "Christian"); 

和响应:

SoapObject reponse=(SoapObject)envelope.getResponse(); 
response.getProperty("name"); 

API SoapObject

+0

嘿,感谢您的回答。不幸的是,Eclipse会给你的每个建议提供错误。对于request.addProperty(“name”,ElementType.STRING_CLASS,“Christian”);是说STRING_CLASS无法解析或不是一个字段....对于第二个response.getProperty(“名称”); Eclipse希望将响应强制转换为SoapObject或KvmSerializable,但都不起作用。 – chrissik 2013-03-06 06:41:54

相关问题