2012-06-12 19 views
0

通過SOAP方法我想從android/eclipse中使用web服務。但是消費web服務的方式是,我必須提供一個輸入,並且它應該從Web服務返回適當的值。如何實現它?在eclipse中的Webservice

Web方法

public class FahrenheitToCelsius { 
public double FahrenheitToCelsius(double Fahrenheit) 
{ 
    return ((Fahrenheit - 32) * 5)/9; 
} 
} 

的.java類

public class Demo_webserviceActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 

     private static String NAMESPACE = "http://tempuri.org/"; 
     private static String METHOD_NAME = "FahrenheitToCelsius"; 
     private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius"; 
     private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"; 

     Button btnFar; 
     EditText txtFar,txtCel; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnFar = (Button)findViewById(R.id.btnFar); 

     txtFar = (EditText)findViewById(R.id.txtFar); 
     txtCel = (EditText)findViewById(R.id.txtCel); 

     btnFar.setOnClickListener(new View.OnClickListener() 
     { 

     public void onClick(View v) 
     { 
     //Initialize soap request + add parameters 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

     //Use this to add parameters 
     request.addProperty("Fahrenheit",txtFar.getText().toString()); 

     //Declare the version of the SOAP request 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     envelope.setOutputSoapObject(request); 
     envelope.dotNet = true; 

     try 
     { 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

      //this is the actual part that will call the webservice 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      // Get the SoapResult from the envelope body. 
      SoapObject result = (SoapObject)envelope.bodyIn; 

      if(result != null) 
      { 
       //Get the first property and change the label text 
       txtCel.setText(result.getProperty(0).toString()); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      } 
     } 
     }); 
    } 
} 
+0

你有什麼搜索/嘗試? –

+0

請稍等,我會上傳我到目前爲止所做的 –

+0

請不要去我需要的解決方案。其我的要求 –

回答

0

從您的網址WSDL和嘗試

私人靜態字符串URL = 「HTTP? //www.w3schools.com/webservices/ tempconvert.asmx「;

+0

你能告訴我爲什麼我想要刪除嗎?WSDL –

+0

不..不,我已經看到了幾個例子,但他們包括一些例子?WSDL .thats爲什麼我問你 –

+0

http://jatin4rise.wordpress.com/2010/10/03/part-2-calling-a-webservice-from-android-application/請訪問這個鏈接的來源... –

相關問題