在Android中使用Web服务
我正在学习如何在android应用程序中实现和使用Web服务。我能从所有使用网络服务的专业人士那里得到一些建议吗?我应该从哪里开始,使用Android应用程序时,哪种方法或类型的网络服务最可靠、最有效 提前谢谢。在Android中使用Web服务,android,performance,android-intent,Android,Performance,Android Intent,我正在学习如何在android应用程序中实现和使用Web服务。我能从所有使用网络服务的专业人士那里得到一些建议吗?我应该从哪里开始,使用Android应用程序时,哪种方法或类型的网络服务最可靠、最有效 提前谢谢。 阿里使用Ksoap有关更多详细信息,请参阅链接。 使用Ksoap了解更多详细信息,请参阅链接。 使用此链接 导入org.ksoap2.SoapEnvelope; 导入org.ksoap2.serialization.SoapObject; 导入org.ksoap2.serial
阿里使用Ksoap有关更多详细信息,请参阅链接。
使用Ksoap了解更多详细信息,请参阅链接。 使用此链接
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类WebServiceDemoActivity扩展活动
{
/**在首次创建活动时调用*/
私有静态字符串SOAP_ACTION1=”http://tempuri.org/FahrenheitToCelsius";
私有静态字符串SOAP_ACTION2=”http://tempuri.org/CelsiusToFahrenheit";
私有静态字符串命名空间=”http://tempuri.org/";
私有静态字符串方法\u NAME1=“华氏摄氏度”;
私有静态字符串方法\u NAME2=“CelsiusToFahrenheit”;
专用静态字符串URL=”http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
按钮btnFar、btnCel、btnClear;
编辑文本txtFar,txtCel;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar=(按钮)findViewById(R.id.btnFar);
btnCel=(按钮)findViewById(R.id.btnCel);
btnClear=(按钮)findviewbyd(R.id.btnClear);
txtFar=(EditText)findViewById(R.id.txtFar);
txtCel=(EditText)findViewById(R.id.txtCel);
btnFar.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间,方法\名称1);
//使用此选项可以添加参数
request.addProperty(“华氏度”,txtFar.getText().toString());
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
调用(SOAP_ACTION1,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null)
{
//获取第一个属性并更改标签文本
setText(result.getProperty(0.toString());
}
其他的
{
Toast.makeText(getApplicationContext(),“无响应”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}
});
btnCel.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间,方法\名称2);
//使用此选项可以添加参数
request.addProperty(“摄氏度”,txtCel.getText().toString());
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
调用(SOAP_ACTION2,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null)
{
//获取第一个属性并更改标签文本
setText(result.getProperty(0.toString());
}
其他的
{
Toast.makeText(getApplicationContext(),“无响应”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}
});
setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
txtCel.setText(“”);
txtFar.setText(“”);
}
});
}
}使用此链接
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类WebServiceDemoActivity扩展活动
{
/**在首次创建活动时调用*/
私有静态字符串SOAP_ACTION1=”http://tempuri.org/FahrenheitToCelsius";
私有静态字符串SOAP_ACTION2=”http://tempuri.org/CelsiusToFahrenheit";
私有静态字符串NAMESPACE=“http:/
<?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("");
}
});
}