在Android中使用Web服务

在Android中使用Web服务,第1张

概述我正在学习如何在我的Android应用程序中实现和使用Web服务.我可以请所有使用Web服务的Pro提出一些建议,我应该从哪里开始,哪种方法或类型的Web服务最可靠,最有效地使用Android应用程序?提前致谢.阿里解决方法:使用此链接http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-ca

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

提前致谢.
阿里

解决方法:

使用此链接
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="@R_301_6822@://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.@R_301_6822@TransportSE;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 = "@R_301_6822@://tempuri.org/FahrenheitToCelsius";  private static String SOAP_ACTION2 = "@R_301_6822@://tempuri.org/CelsiusToFahrenheit";  private static String nameSPACE = "@R_301_6822@://tempuri.org/";  private static String METHOD_name1 = "FahrenheitToCelsius";  private static String METHOD_name2 = "CelsiusToFahrenheit";  private static String URL =               "@R_301_6822@://www.w3schools.com/webservices/tempconvert.asmx?WSDL";  button btnFar,btnCel,btnClear;  EditText txtFar,txtCel;@OverrIDepublic 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 {                    @R_301_6822@TransportSE androID@R_301_6822@Transport = new @R_301_6822@TransportSE(URL);                    //this is the actual part that will call the webservice                    androID@R_301_6822@Transport.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 {                    @R_301_6822@TransportSE androID@R_301_6822@Transport = new @R_301_6822@TransportSE(URL);                    //this is the actual part that will call the webservice                    androID@R_301_6822@Transport.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("");              }        });}

}

总结

以上是内存溢出为你收集整理的在Android中使用Web服务全部内容,希望文章能够帮你解决在Android中使用Web服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102415.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存