通过Android应用程序调用Web服务

时间:2013-01-31 10:45:54

标签: android web-services

我正在android中创建一个虚拟应用程序。这个应用程序从Web服务调用一个方法。该方法用于添加数字。我提供两个整数输入,它应该返回一个整数值,但它输出0作为输出(无论输入是什么) 。请帮帮我。

下面是我的java代码

public class MainActivity extends Activity {

private static String SOAP_ACTION = "http://tempuri.org/Add";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "Add";
private static String URL = "http://192.168.0.136/asmxfirst/Calculation.asmx?WSDL";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editText1 = (EditText) findViewById(R.id.editText1);
    final EditText editText2 = (EditText) findViewById(R.id.editText2);
    final EditText editText3 = (EditText) findViewById(R.id.editText3);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("a", Integer.parseInt((editText1.getText().toString())));
            request.addProperty("b", Integer.parseInt((editText2.getText().toString())));   

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            try {
             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
             androidHttpTransport.call(SOAP_ACTION, envelope);
             Object result = envelope.getResponse();
             if(result != null)
             {
                  String[] results = (String[]) result;
                   editText3.setText(results[0]);
             }
             else
             {
                   Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
             }
            } 
            catch (Exception e) {
             e.printStackTrace();
                }

        }
    }) ;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}       

}

2 个答案:

答案 0 :(得分:0)

使用此链接http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAppearance="?android:attr/textAppearanceLarge" />

  <EditText
android:id="@+id/txtFar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<requestFocus />
    </EditText>

     <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius"
android:textAppearance="?android:attr/textAppearanceLarge" />

         <EditText
android:id="@+id/txtCel"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

     <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
    android:id="@+id/btnFar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Convert To Celsius" />

<Button
    android:id="@+id/btnCel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Convert To Fahrenheit" />

 </LinearLayout>


  <Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />

 </LinearLayout>

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import android.app.Activity;
   import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

     public class WebServiceDemoActivity extends Activity
     {
       /** Called when the activity is first created. */
   private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
  private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
  private static String NAMESPACE = "http://tempuri.org/";
  private static String METHOD_NAME1 = "FahrenheitToCelsius";
   private static String METHOD_NAME2 = "CelsiusToFahrenheit";
   private static String URL ="http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

   Button btnFar,btnCel,btnClear;
    EditText txtFar,txtCel;

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

btnFar = (Button)findViewById(R.id.btnFar);
btnCel = (Button)findViewById(R.id.btnCel);
btnClear = (Button)findViewById(R.id.btnClear);
txtFar = (EditText)findViewById(R.id.txtFar);
txtCel = (EditText)findViewById(R.id.txtCel);

btnFar.setOnClickListener(new View.OnClickListener()
{
          @Override
          public void onClick(View v)
          {
                //Initialize soap request + add parameters
          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);       

          //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_ACTION1, 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();
          }
          }
    });

btnCel.setOnClickListener(new View.OnClickListener()
{
          @Override
          public void onClick(View v)
          {
                //Initialize soap request + add parameters
          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);       

          //Use this to add parameters
          request.addProperty("Celsius",txtCel.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_ACTION2, 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
                      txtFar.setText(result.getProperty(0).toString());
                }
                else
                {
                      Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
                }
          } catch (Exception e) {
                e.printStackTrace();
          }
          }
    });

btnClear.setOnClickListener(new View.OnClickListener()
{
          @Override
          public void onClick(View v)
          {
                txtCel.setText("");
                txtFar.setText("");
          }
    });

}

答案 1 :(得分:0)

使用soapobject

SoapObject result = (SoapObject)envelope.bodyIn;

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