在Android中使用Web服务Android、Web

2023-09-12 06:50:53 作者:感情的比赛我永远在淘汰赛

我学习如何实现和在我的Andr​​oid应用程序使用的Web服务。我可以请有来自临谁正在使用Web服务提出一些建议,认为应在那里我开始和这方法或类型的Web服务是最可靠,最有效地使用Android应用程序?

I am learning how to implement and use Web Services in my android Application. Can I please have some suggestions from all Pro who are using web services, that where shall I start and which method or type of web services are most reliable and efficient to use Android Applications?

在此先感谢。 阿里

推荐答案

使用这个链接 的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("");
              }
        });
}

}

 
精彩推荐
图片推荐