2013-01-22 46 views
1

我是新來的android和我想嘗試一個Web服務的例子。 我已經使用了ksoap2庫。 我跟着一個視頻和類型下面的代碼:錯誤getResponse()

package example.web; 

import android.R.anim; 
import android.app.Activity; 
import android.os.Bundle; 
import org.ksoap2.*; 
import org.ksoap2.serialization.*; 
import org.ksoap2.transport.*; 
import android.widget.TextView; 

public class First extends Activity { 
/** Called when the activity is first created. */ 
    TextView tv; 
    private static final String    SOAP_ACTION="http://tempuri.org/CelsiusToFahrenheit"; 
    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"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv = (TextView) findViewById(R.id.textView1); 

     // creating the soap request and all its paramaters 
     SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("Celsius","32");// hardcoding some random value 
     //we can also take in a value in a var and pass it there 


     //set soap envelope,set to dotnet and set output 
     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     soapEnvelope.dotNet=true; 
     soapEnvelope.setOutputSoapObject(request); 

     HttpTransportSE obj = new HttpTransportSE(URL); 
     //to make call to server 
     try { 
      obj.call(SOAP_ACTION,soapEnvelope);// in out parameter 
     SoapPrimitive resultString=(SoapPrimitive)SoapEnvelope.getResponse(); 
     tv.setText("Status : " + resultString); 
     } 
     catch(Exception e) { 
     e.printStackTrace(); 
     } 
    } 
} 

SoapPrimitive resultString=(SoapPrimitive)SoapEnvelope.getResponse(); 

在上述行,我上getResponse(); 請人幫我剛開錯誤。

+0

你得到了什麼錯誤? – Infinity

回答

-1

試圖改變自己的SoapEnvelopesoapEnvelope和進去SoapObject,而不是SoapPrimitive

try 
{ 
    obj.call(SOAP_ACTION,soapEnvelope);// in out parameter 
    SoapObject resultString = (SoapObject) soapEnvelope.getResponse(); 
    tv.setText("Status : " + resultString); 
} 
catch (Exception e) 
{ 
    SoapObject resultString = (SoapObject)soapEnvelope.bodyIn; 
} 

希望它可以幫助你出來的結果!

+0

嘿,我設法挑出這個錯誤,但仍然應用程序不斷崩潰,只要我在模擬器上啓動它,沒有我能看到的語法錯誤。請通過我的代碼,並指導我通過任何其他錯誤。 :) –

相關問題