Test測試(匯入阿里巴巴類庫 maven { url "https://oss.sonatype.org/content/repositories/ksoap2-android-releases/)
阿新 • • 發佈:2019-02-09
匯入類庫
// 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(),結構為:城市名稱(城市程式碼)。 } }
點選此處執行