0

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

1 回答 1

0

从您的 URL 中删除 ?WSDL 并尝试

私有静态字符串 URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

于 2012-06-12T05:21:46.847 回答