如何正确地从android请求web服务?
我正在尝试使用web服务:如何正确地从android请求web服务?,android,web-services,soap,Android,Web Services,Soap,我正在尝试使用web服务: @WebService(serviceName = "SayHelloService") public class SayHelloService { @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { //return "Hello " + txt + " !"; return "A
@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) {}
}
}
我的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属性的方式有问题。
你能告诉我是什么问题吗?
更新:我收到的错误消息是:EconRefused(连接被拒绝)我发现问题是因为我在android emulator上使用了localhost 这在这里解释
谢谢大家的努力 如果您正在编写服务器和客户端,我强烈建议使用rest和json,因为soap和xml对于移动设备来说体积庞大,处理速度慢,在移动网络上传输速度慢。您能发布您收到的错误消息吗?它们可能是问题的一个指标,我知道休息的简单性。我当然更喜欢休息。然而,我也在尝试学习SOAP,这导致了这个练习。我从异常中得到的错误消息是:econnreference(连接被拒绝)。
<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>