0

我正在学习如何在我的 android 应用程序中实现和使用 Web 服务。我能否请所有正在使用 Web 服务的 Pro 提供一些建议,我应该从哪里开始以及哪种方法或类型的 Web 服务最可靠和最有效地使用 Android 应用程序?

提前致谢。阿里

4

3 回答 3

3

使用 Ksoap 了解更多详细信息,请参阅链接。 http://codeoncloud.blogspot.in/2012/04/android-403-webservice-access-tutorial.html

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

于 2013-01-28T13:44:26.967 回答
1

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

}

于 2013-01-28T13:45:54.767 回答
0

对于我从事过的企业应用程序,它们中的大多数使用 RESTful 移动服务层,旨在消除让服务器保持应用程序状态(因为手机很容易失去连接)。

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

这是一个很棒的 Chrome 插件,您可以使用它来测试和使用 RESTful Web 服务:

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

SOAP 是另一种选择,也用于一些移动应用程序。

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

于 2013-01-28T13:49:47.627 回答