2014-10-07 85 views
1

晚上好,我很長時間試圖消費一個simpes WS,已經研究並嘗試了不同的方法來解決這個錯誤並失敗,嘗試了不同版本的kSOAP,2.5。 2,2.6.0,3.0.0,3.3,0。格斯塔里亞愛知道如何解決。每次我來「transporte.call(SOAP_ACTION sobre);」生成一個空的異常。 PS:WS是免費的,這就是,如果他們要訪問的鏈接: http://www.w3schools.com/webservices/tempconvert.asmxAndroid消費WebService,異常HttpTransportSE.call()

,這是我的代碼:

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()); 
    } 

} 

感謝分享知識!

回答

0

你有錯命名空間,方法和肥皂行動

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()); 
    } 

} 

的問題是,你主要不能做網絡操作線。再加上一些命名空間和其他常量問題。

+0

你好嗶嘰,我改變了 「命名空間,方法,肥皂行動」,並沒有得到解決, 保持方法例外 「.CALL()」 – Muller 2014-10-07 19:42:09

+0

我查看了WSDL,它實際上顯示了'private static final String NAMESPACE =「http://www.w3schools.com/webservices/」;'最後用斜槓。你可以試試這個嗎? – serge 2014-10-07 20:17:17

+0

我剛剛編輯回答添加這個斜槓 – serge 2014-10-07 20:25:47

0

從嗶嘰工作的代碼,但需要的東西

沒有看到線程,這樣,需要警方在主胎面訪問添加此

`StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder() .permitAll()建立();

StrictMode.setThreadPolicy(policy);

或使用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 
} 

}