Java 使用SOAP服务
我正在尝试连接到w3school.com的SOAP服务,但它不起作用 首先,我尝试了我在教程中阅读的方式。这样我得到了一个Java 使用SOAP服务,java,android,soap,android-ksoap2,ksoap,Java,Android,Soap,Android Ksoap2,Ksoap,我正在尝试连接到w3school.com的SOAP服务,但它不起作用 首先,我尝试了我在教程中阅读的方式。这样我得到了一个流关闭异常 然后我尝试使用KeepAliveHttpStranSports类。这样我就得到了另一个异常 org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope (position:START_TAG <h
流关闭异常
然后我尝试使用KeepAliveHttpStranSports
类。这样我就得到了另一个异常
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope (position:START_TAG <html lang='en-US'>@3:20 in java.io.InputStreamReader@f298f58) e.printStackTrace();
问题出在w3school服务中,而不是代码中。请尝试访问其他服务
public class MainActivity extends AppCompatActivity {
private Context sContext;
// SOAP TEST
private static final String SOAP_ACTION = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "https://www.w3schools.com/xml/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sContext = this;
myAsyncTask myRequest = new myAsyncTask();
myRequest.execute();
}
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String URL = "http://www.w3schools.com/xml/tempconvert.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "48"); // adding method property here serially
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
KeepAliveHttpsTransportSE keepAliveHttpsTransportSE = new KeepAliveHttpsTransportSE("www.w3schools.com", 443, "", 30000);
keepAliveHttpsTransportSE.debug = true;
//HttpTransportSE httpTransport = new HttpTransportSE(URL);
//httpTransport.debug = true;
Log.i("mmb Envelop", envelope.toString());
try {
//httpTransport.call(SOAP_ACTION, envelope);
keepAliveHttpsTransportSE.call(SOAP_ACTION, envelope);
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
Log.e("mmb HTTPLOG", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("mmb IOLOG", e.getMessage());
// Message: java.io.IOException: Stream closed
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
Log.e("mmb XMLLOG", e.getMessage());
// Message: org.xmlpull.v1.XmlPullParserException: expected:
// START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope
// (position:START_TAG <html lang='en-US'>@3:20 in java.io.InputStreamReader@f298f58)
e.printStackTrace();
} //send request
Object result = null;
try {
result = (Object )envelope.getResponse();
Log.i("mmb RESPONSE",String.valueOf(result)); // see output in the console
} catch (SoapFault e) {
// TODO Auto-generated catch block
Log.e("mmb SOAPLOG", e.getMessage());
e.printStackTrace();
}
return null;
}
}
}