Android连接服务端WebService基础
移动应用手机App开发,必须连接服务端各种API应用,才能使得移动App拥有超强的服务,本文介绍Android对接Webservice的基础部分,开发环境Android Studio 3.4.1,Webservice服务端采用.NET环境,本文示例适合有一定java基础,对Android开发有基本的入门,对WebService有简单了解,对.NET或Java环境下的WebService有所了解更容易上手。
在Android Studio中新建项目,菜单:File文件 > New新建 > News Project新建项目,在“Choose your project”中选择“Phone and Tablet”项(手机app),列表中选“Empty Activity”空白Activity,然后"Configure your project"定义项目的名称和项目路径,点击“Finish”完成建立项目。
进入activity_main.xml将默认的“ConstraintLayout”约束布局改成“LinearLayout”线性布局;将<TextView>的android:layout_width="wrap_parent"改成android:layout_width="match_parent",让TextView的宽和父级同样宽,activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_GetTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
activity_main.xml的设计视图:
MainActivity.java后台代码,连接WebService程序代码,在项目中引入ksoap2.5.4.jar包,具体操作将ksoap2.5.4.jar拷贝粘贴至Android Studio的app>libs中(务必让项目切换成“Project”模式下),然后右键libs下的ksoap2.5.4.jar,点击“Add library…”引入到项目中,这样在后台Java程序中才能调用。步骤如下图。
WebService部分:
在本文示例中Webservice服务端是.NET Framek环境,用C#编写的程序,用Java环境写的Webservice也是可以的,为了便于演示这把WebService程序已经发布到“http://www.weisim3.com/api/TestService.asmx”;在TestService.asmx下有一个“GetTime”方法,将它作为“Android连接服务端WebService基础”的入门第一个示例,通过“GetTime”返回服务端时间。代码如下:
MainActivity.java后台代码中引入http://www.weisim3.com/api/TestService.asmx, 定义公共变量:public final String SOAP_ADDRESS(WebService的引入地址),public final String WSDL_TARGET_NAMESPACE(WebService的命名空间), public final String SOAP_ACTION (WebService调用方法的路径名 ),public final String OPERATION_NAME(WebService调用的方法名)。如下图所示:
接着定义“public String Call()”方法,调用详细的Webservice地址和方法,返回String类型的Webservice结果,在"Call()"方法中定义局部变量SoapObject request、 SoapSerializationEnvelope envelope 、HttpTransportSE httpTransport,分别引入ksoap2.5.4.jar包的命名空间import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.ksoap2.SoapEnvelope; protected void onCreate方法中建立线程,然后在线程中获取Call()方法结果,赋值给TextView的setText。 代码如下:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
/*
Android连接服务端WebService基础
Copyright (C) 遗昕 | weisim3.com 02.05.2020
*/
public final String SOAP_ADDRESS = "http://www.weisim3.com/api/TestService.asmx";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ACTION = "http://tempuri.org/GetTime";
public final String OPERATION_NAME = "GetTime";
/*Webservice Call -Webservice调用
Copyright (C) 遗昕 | weisim3.com 02.05.2020*/
public String Call()
{
/*SoapObject request传入Webservice方法地址和方法名*/
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable(){
@Override
public void run() {
TextView t=findViewById(R.id.text_GetTime);
String str="<h3 style='#FF0000'>"+Call()+"</h3>";
t.setText(Html.fromHtml(str));
}
}).start(); /* 启用线程中调用Call()方法*/
}
AndroidManifest.xml全局配置:在manifest节点内加入<uses-permission android:name="android.permission.INTERNET"/>,在application节点加入android:usesCleartextTraffic="true"配置,详细解释可参考《Android 连接Api webservice几个关键问题》,详细代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.weisim3client" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最终效果如下: