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 を削除して試してください

private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

于 2012-06-12T05:21:46.847 に答える