如何从android正确请求Web服务?

时间:2013-12-29 01:16:03

标签: android web-services soap

我正在尝试使用网络服务:

@WebService(serviceName = "SayHelloService")
public class SayHelloService {
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        //return "Hello " + txt + " !";
        return "Application submitted successfully!";
    }
}

来自Android应用:

public class MainActivity extends Activity {
    Button b;
    TextView tv;
    String editText = "me";
    String displayText; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.submit_button);
        tv = (TextView) findViewById(R.id.ws_response_string);
        b.setOnClickListener(new OnClickListener() 
        {
               public void onClick(View v) 
               {               
                   AsyncCallWS task = new AsyncCallWS();
                   task.execute();
               }
           });
    private class AsyncCallWS extends AsyncTask<String, Void, Void> 
    {
        @Override
        protected Void doInBackground(String... params) 
        {                
            //Invoke webservice                
            displayText = WebService.invokeHelloWorldWS(editText,"hello");
            return null;
        }
        @Override
        protected void onPostExecute(Void result) 
        {
            tv.setText(displayText);
        }
        @Override
        protected void onPreExecute() {}
        @Override
        protected void onProgressUpdate(Void... values) {}
    }
}

具有以下属性:

public class WebService 
{
    private static String NAMESPACE = "http://example.vnsgbt.org/";
    private static String URL = "http://localhost:8080/HelloWorldWebService/SayHelloService?wsdl";
    private static String SOAP_ACTION = "http://example.vnsgbt.org/";
    public static String invokeHelloWorldWS(String name, String webMethName) 
    {
        String resTxt = null;
        SoapObject request = new SoapObject(NAMESPACE, webMethName);
        PropertyInfo sayHelloPI = new PropertyInfo();
        sayHelloPI.setName("name");
        sayHelloPI.setValue(name);
        sayHelloPI.setType(String.class);
        request.addProperty(sayHelloPI);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try 
        {
            androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            resTxt = response.toString();

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            //resTxt = "Error occurred";
            resTxt = e.getMessage();
        } 
        return resTxt;
    }
}

我的wsdl看起来像:

<types>
<xsd:schema>
<xsd:import namespace="http://example.vnsgbt.org/" schemaLocation="http://localhost:8080/HelloWorldWebService/SayHelloService?xsd=1"/>
</xsd:schema>
</types>
<message name="hello">
<part name="parameters" element="tns:hello"/>
</message>
<message name="helloResponse">
<part name="parameters" element="tns:helloResponse"/>
</message>
<portType name="SayHelloService">
<operation name="hello">
<input wsam:Action="http://example.vnsgbt.org/SayHelloService/helloRequest" message="tns:hello"/>
<output wsam:Action="http://example.vnsgbt.org/SayHelloService/helloResponse" message="tns:helloResponse"/>
</operation>
</portType>
<binding name="SayHelloServicePortBinding" type="tns:SayHelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="hello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SayHelloService">
<port name="SayHelloServicePort" binding="tns:SayHelloServicePortBinding">
<soap:address location="http://localhost:8080/HelloWorldWebService/SayHelloService"/>
</port>
</service>

我不断收到来自异常的错误消息,而不是来自Web服务的成功消息。 我猜测我设置SOAP属性的方式有问题。 你能说明问题是什么吗? 更新:我收到的错误消息是:ECONNREFUSED(拒绝连接)

1 个答案:

答案 0 :(得分:0)

我发现问题是因为我在android模拟器上使用localhost。

这里解释了https://stackoverflow.com/a/13866889/1971025

感谢大家的努力!