Java Android消费Web服务,异常为HttpTransportSE.call()
晚上好,我花了很长时间尝试使用一个simpes WS,已经研究并尝试了不同的方法来解决此错误,但失败了,尝试了不同版本的kSOAP,2.5.2,2.6.0,3.0.0,3.3,0。。戈斯塔里亚喜欢知道如何解决问题。每次我来时,“transporte.call(SOAP_ACTION sobre);”都会生成一个空异常。 PS:WS是免费的,这是他们想要访问的链接: 这是我的代码:Java Android消费Web服务,异常为HttpTransportSE.call(),java,android,web-services,ksoap,Java,Android,Web Services,Ksoap,晚上好,我花了很长时间尝试使用一个simpes WS,已经研究并尝试了不同的方法来解决此错误,但失败了,尝试了不同版本的kSOAP,2.5.2,2.6.0,3.0.0,3.3,0。。戈斯塔里亚喜欢知道如何解决问题。每次我来时,“transporte.call(SOAP_ACTION sobre);”都会生成一个空异常。 PS:WS是免费的,这是他们想要访问的链接: 这是我的代码: private static final String SOAP_ACTION = "http://tempuri
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusT oFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
try{
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sobre.dotNet = true;
sobre.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL, 30000);
transporte.call(SOAP_ACTION, sobre);
SoapPrimitive resultado = (SoapPrimitive)sobre.getResponse();
tv.setText("" + resultado.toString());
}catch(Exception e){
tv.setText(e.getMessage());
}
}
谢谢分享知识 您得到了错误的名称空间、方法和soap操作
private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
希望能有帮助
编辑:
这项工作:
private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
//private static final String NAMESPACE = "http://www.w3schools.com/webservices";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
//private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
//private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
String result = "";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
final SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sobre.dotNet = true;
sobre.setOutputSoapObject(request);
final HttpTransportSE transporte = new HttpTransportSE(URL, 30000);
new Thread(new Runnable() {
@Override
public void run() {
try {
transporte.call(SOAP_ACTION, sobre);
SoapPrimitive resultado = (SoapPrimitive)sobre.getResponse();
result = resultado.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
int countWait = 5;
while (result.isEmpty() && countWait > 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
countWait--;
}
tv.setText(result);
}catch(Exception e){
tv.setText(e.getMessage());
}
}
问题是您无法在主线程中执行网络操作。加上一些名称空间和其他常量问题。来自serge的代码可以工作,但需要一些东西 不要看到线程所以,需要警察在主踏板访问添加这个 `StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build() StrictMode.setThreadPolicy(策略) 或者使用asinctask
class DemoTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
//Your implementation
}
protected void onPostExecute(Void result) {
// TODO: do something with the feed
}
类DemoTask扩展异步任务{
受保护的Void doInBackground(Void…arg0){
//您的实现
}
受保护的void onPostExecute(void结果){
//TODO:对提要做些什么
}
}Hello Serge,我更改了“名称空间、方法、soap操作”,但没有解决,仍然是方法中的异常。call()“我查看了WSDL,它实际上显示了
私有静态最终字符串namespace=”http://www.w3schools.com/webservices/";代码>结尾带有斜杠。你能试试这个吗?你能检查Transport是否为空吗?你能给你的问题添加堆栈吗?是的,transporta返回空异常,你有电子邮件吗?如果你想,我可以发送你的电子邮件项目