导入类库
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/ksoap2-android-releases/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
导入依赖
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.lianxi"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
当前的这个类中进行编写数据
package com.example.a24476.fweek;
import android.util.Log;
import org.junit.Test;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
//命名空间
String nameSpace = "http://WebXml.com.cn/";
//地址
String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
//junit 测试 java测试
@Test public void test() throws IOException, XmlPullParserException {
//System.out.println("测试运行");
//mvp okhttp retrofit rxjava
//mvp -> 便于分开测试s
//Log.i("TEST", "test");
String methodName = "getSupportProvince";
//创建HttpTransportSe对象:请求url地址
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
//创建SoapObject对象:命名空间,调用方法名
SoapObject soapObject = new SoapObject(nameSpace, methodName);
//soapObject.addProperty("", "");
//传递参数 没有
//Soap序列化对象:版本
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
//设置
soapSerializationEnvelope.dotNet = true;//是否支持.net
//设置输入的简单对象
soapSerializationEnvelope.setOutputSoapObject(soapObject);
httpTransportSE.debug = true;//开启调试
//执行调用: 命名空间 + 调用方法
httpTransportSE.call(nameSpace + methodName, soapSerializationEnvelope);
//获取结果
if(soapSerializationEnvelope.getResponse() != null) {
SoapObject bodyIn = (SoapObject) soapSerializationEnvelope.bodyIn;
//ctrl + alt + v / f 抽 本地 成员
SoapObject property = (SoapObject) bodyIn.getProperty(0);
for (int i = 0; i < property.getPropertyCount(); i++) {
SoapPrimitive soapPrimitive = (SoapPrimitive) property.getProperty(i);
System.out.println(soapPrimitive.getValue());
}
System.out.println(bodyIn);
}
}
//
@Test public void get() {
//请求支持的城市信息
//getSupportCity
//查询本天气预报Web Services支持的国内外城市或地区信息
//输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市;返回数据:一个一维字符串数组 String(),结构为:城市名称(城市代码)。
}
}
点击此处运行