簡體   English   中英

android eclipse中的Webservice

[英]Webservice in android eclipse

我想通過SOAP方法從android / eclipse中使用Web服務。但是使用Web服務的方式是,我必須提供輸入,並且它必須從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();
           }
         }
       });
    }
}

從您的URL中刪除?WSDL並嘗試

私有靜態字符串URL =“ http://www.w3schools.com/webservices/tempconvert.asmx”;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM