工具准备:
OPPO k3 Android (9.0)手机,http://www.webxml.com.cn 天气测试网站, Android studio 3.2开发环境
实现步骤:
- 建立Android工程 增加输入,显示框和按钮
- 下载:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar 包
- 把第二步下载的包拷贝到工程目录app/libs 下
- 在Android studio 添加包到工程
- 编写代码调用公开的网络天气接口验证工程代码。
- 修改代码调用公司后台服务器接口。
具体实现
1. 建立工程,输入,显示框和按钮 满足测试需要如图:
2.下载soap包
3.复制到相应目录如图:
4.以添加第三方模块的方式添加到工程如图:
5.完成天气网站可支持省份接口的调用:如图
6.确认soap正常工作,更换公司内网,修改代码调用公司后台服务:(代码跟Linux 端调用区别较大,具体参看工程代码):如图
package com.example.superman.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.AndroidHttpTransport;
public class MainActivity extends AppCompatActivity {
String TAG = "Response";
Button bt;
EditText celcius;
TextView txt;
String getCel;
SoapPrimitive resultString;
//命名空间
private static final String serviceNameSpace="http://tempuri.org/";
String xx="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
celcius = (EditText) findViewById(R.id.cel);
txt = (TextView) findViewById(R.id.txt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getCel = celcius.getText().toString();
AsyncCallWS task = new AsyncCallWS();
task.execute();
txt.setText(xx);
//calculate();
}
});
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
calculate();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
// Toast.makeText(MainActivity.this, "Response" + resultString.toString(), Toast.LENGTH_LONG).show();
}
}
public void calculate() {
String serviceURL="http://10.0.5.47:9080/TmriOutAccess.svc?singleWsdl";
String getSupportCity="getSupportCity";
String getSupportProvince="getSupportProvince";
String query="queryObjectOut";
String para = "<?xml version=\"1.0\" encoding=\"gb2312\"?> <root> <QueryCondition> <SN>123456</SN> </QueryCondition></root> ";
try {
//SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
///Request.addProperty("Celsius", getCel);
SoapObject soapObject = new SoapObject("http://tempuri.org/", query);
soapObject.addProperty("xtlb","17");
soapObject.addProperty("jkxlh","17J01");
soapObject.addProperty("jkid","17J01");
soapObject.addProperty("xmlDoc",para);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(soapObject);
HttpTransportSE transport = new HttpTransportSE(serviceURL);
transport.debug=true;
transport.call("http://tempuri.org/ITmriOutAccess/queryObjectOut", soapEnvelope);
Object result = (Object) soapEnvelope.getResponse();
Log.i(TAG, "Result Celsius: " + result);
xx = result.toString();
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
}
}
上面就是测试的简单程序,如果做过linux 下的soap 调用应该会熟悉一些,在Android下不需要引用获得文件,这是比较大的区别。
再一个就是演示 namespace 的确定和 参数的添加(soapObjetc.addProperty的使用),我也在这里来回实验,debug 根据错误信息修改代码和相应的参数才试出来。