Using Web Services in Android [closed]

2019-01-12 08:11发布

问题:

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?

Thanks in advance. Ali

回答1:

use this link 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("");
              }
        });
}

}



回答2:

Use Ksoap for more details refer the links. http://codeoncloud.blogspot.in/2012/04/android-403-webservice-access-tutorial.html

http://codeoncloud.blogspot.in/2012/04/android-web-service-access-tutorial.html



回答3:

With enterprise applications I have worked on, most of them use a RESTful mobile service tier, designed to remove having the server keep state for the application (since phones can lose connections quite easily).

http://www.ibm.com/developerworks/webservices/library/ws-restful/

Here's great plugin for Chrome you can use to test and work with RESTful web services:

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?utm_source=ha-en-na-us-webapp-Advanced%20Rest%20Client%20Application

SOAP is another option, and is used in some mobile applications as well.

http://en.wikipedia.org/wiki/SOAP