晚上好,我花了很长时间尝试使用一个简单的WS,已经研究并尝试了不同的方法来解决这个错误,但失败了,尝试了不同版本的kSOAP,2.5.2,2.6.0,3.0.0,3.3,0。..Gostaria喜欢知道如何解决问题。每次我调用"transporte.call (SOAP_ACTION sobre);“都会生成一个空异常。PS: WS是免费的,这是他们想要访问的链接:http://www.w3schools.com/webservices/tempconvert.asmx
这是我的代码:
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());
}
}感谢你分享这些知识!
发布于 2014-10-07 11:11:39
您获得了错误的命名空间、方法和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());
}
}问题是你不能在主线程中进行网络操作。加上一些命名空间和其他常量问题。
发布于 2015-07-10 22:18:03
serge中的代码可以工作,但需要一些东西
看不到线程所以,需要警察在主要踏板访问添加此
StrictMode.ThreadPolicy.Builder().permitAll().build();策略=新的`StrictMode.ThreadPolicy
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
}}
https://stackoverflow.com/questions/26226864
复制相似问题