-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1,精简依赖 2,分为2部分:ksoap转换类,retrofit工具类。
- Loading branch information
wangkezhi
committed
May 22, 2017
1 parent
4d004ff
commit 42e2da5
Showing
31 changed files
with
606 additions
and
1,095 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
# ksoap2retrofit | ||
you use retrofit2 access webservice ,replace ksoap jar. | ||
It is can help you use okhttp3 or retrofit accsse webservice. | ||
KSoap2RetrofitHelper can convert httprequest to ksoaprequest and convert ksoap result to normal data,don't include netframwork denpence. | ||
KSoap2JsonConverterFactory can help you that convert ksoap result to json when you use retrofit. | ||
KSoap2XmlConverterFactory can help you that convert ksoap result to simplexml when you use retrofit. | ||
|
||
|
||
## Base usage | ||
#### first,you need convert ksoap header ,nameSpace,method,key-value to normal header and request body. | ||
/** | ||
* 转换ksoap请求头和请求体 | ||
* | ||
* @param method 方法名 | ||
* @param nameSpacre 命名空间 | ||
* @param properties 键值对 | ||
* @return 转换后的请求头和请求体 | ||
*/ | ||
Requestksoap requestksoap = KSoap2RetrofitHelper.getInstance().convertRequest(method, nameSpacre, key-value); | ||
#### secend,you need conver ksoap response to normal data. | ||
## when you don't use retrofit,the method can conver ksoap response to the String data response. | ||
/** | ||
* 转换ksoap响应体 | ||
* | ||
* @param value 响应体 | ||
* @return 转换后的响应体 | ||
* @throws IOException | ||
*/ | ||
String normalresponse=KSoap2RetrofitHelper.getInstance().convertRequest(ksoapresponse); | ||
#### | ||
## when you use retrofit,you can use convertfactor tool. | ||
KSoap2XmlConverterFactory is xml tool. | ||
KSoap2JsonConverterFactory is json tool. | ||
|
||
## usage | ||
#### 1,set ConfigManager,include of baseurl,timeout field. | ||
|
||
ConfigManager configManager = new ConfigManager.Builder().setBASE_URL(ConstantManager.baseurl).build(); | ||
#### 2,init retrofit. | ||
|
||
RetrofitManager manager = RetrofitManager.getInstance().init(configManager); | ||
#### 3,convert ksoap string to the retrofit header and body. | ||
|
||
HashMap<String, Object> properties = new HashMap<String, Object>(); | ||
properties.put("loginName", username); | ||
properties.put("password", pwd); | ||
properties.put("code", ""); | ||
List<Object> getParamters = SoapHelper.getInstance().getParams(ConstantManager.loginMethodName, ConstantManager.nameSpace, properties); | ||
Map<String, String> soapHeaderMap = null; | ||
String mBody = null; | ||
if (getParamters != null) { | ||
soapHeaderMap = (Map<String, String>) getParamters.get(0); | ||
mBody = new String((byte[]) getParamters.get(1)); | ||
} | ||
return getPams4Api().downloadbasedata(soapHeaderMap, mBody); | ||
#### 4,make your modle.it will be parsed by simplexml. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 25 | ||
buildToolsVersion "25.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 15 | ||
targetSdkVersion 25 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
|
||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(include: ['*.jar'], dir: 'libs') | ||
provided('org.simpleframework:simple-xml:2.7.1') { | ||
exclude group: 'xpp3', module: 'xpp3' | ||
exclude group: 'stax', module: 'stax-api' | ||
exclude group: 'stax', module: 'stax' | ||
} | ||
provided 'com.google.code.gson:gson:2.8.0' | ||
provided 'com.squareup.retrofit2:retrofit:2.1.0' | ||
provided project(':ksoap2retrofit') | ||
//provided 'com.squareup.okhttp3:okhttp-urlconnection:3.8.0' | ||
//provided 'com.squareup.retrofit2:retrofit-converters:2.3.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in D:\Android\SDK\sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
...c/androidTest/java/com/ximencx/ksoap2retrofit_convertfactory/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ximencx.ksoap2retrofit_convertfactory; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumentation test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() throws Exception { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("com.ximencx.ksoap2retrofit_convertfactory.test", appContext.getPackageName()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
ksoap2retrofit-convertfactory/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
|
||
package="com.ximencx.ksoap2retrofit_convertfactory"> | ||
|
||
<application android:allowBackup="true" android:label="@string/app_name" | ||
android:supportsRtl="true"> | ||
|
||
</application> | ||
|
||
</manifest> |
38 changes: 38 additions & 0 deletions
38
.../com/ximencx/ksoap2retrofit_convertfactory/convertfactory/KSoap2JsonConverterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ximencx.ksoap2retrofit_convertfactory.convertfactory; | ||
|
||
|
||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import okhttp3.RequestBody; | ||
import okhttp3.ResponseBody; | ||
import retrofit2.Converter; | ||
import retrofit2.Retrofit; | ||
|
||
/** | ||
* author:wangkezhi | ||
* date: 2017/5/19 11:17 | ||
* email:[email protected] | ||
* summary:自定义转换器 | ||
*/ | ||
|
||
public class KSoap2JsonConverterFactory<T> extends Converter.Factory { | ||
private String TAG = getClass().getSimpleName(); | ||
public static KSoap2JsonConverterFactory create() { | ||
return new KSoap2JsonConverterFactory(); | ||
} | ||
|
||
|
||
@Override | ||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, | ||
Retrofit retrofit) { | ||
return new Soap2JsonResponseBodyConverter<Type>(type); | ||
} | ||
|
||
|
||
@Override | ||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { | ||
return new SoapRequestBodyConverter<Type>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../ximencx/ksoap2retrofit_convertfactory/convertfactory/Soap2JsonResponseBodyConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.ximencx.ksoap2retrofit_convertfactory.convertfactory; | ||
|
||
import android.util.Log; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.ximencx.ksoap2retrofit.KSoap2RetrofitHelper; | ||
import com.ximencx.ksoap2retrofit.SoapUtil; | ||
import com.ximencx.ksoap2retrofit.ksoap2.SoapEnvelope; | ||
import com.ximencx.ksoap2retrofit.ksoap2.serialization.SoapObject; | ||
import com.ximencx.ksoap2retrofit.ksoap2.serialization.SoapSerializationEnvelope; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
import okhttp3.ResponseBody; | ||
import retrofit2.Converter; | ||
|
||
/** | ||
* Copyright (c) 2016. 东华博育云有限公司 Inc. All rights reserved. | ||
* com.mr_sun.logindemo.convert | ||
* 作者:Created by sfq on 2016/12/2. | ||
* http://www.jianshu.com/p/6fc8c9081c64 | ||
* 联系方式: | ||
* 功能描述: | ||
* 修改:无 | ||
*/ | ||
public final class Soap2JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> { | ||
|
||
private String TAG = getClass().getSimpleName(); | ||
|
||
private Class<?> clazz; | ||
|
||
protected Soap2JsonResponseBodyConverter(Type clazz) { | ||
this.clazz = (Class<?>) clazz; | ||
} | ||
|
||
@Override | ||
public T convert(ResponseBody value) throws IOException { | ||
//value 参考soap的返回,截取其中的字符串json | ||
SoapEnvelope soapEnvelope = new SoapSerializationEnvelope(KSoap2RetrofitHelper.getInstance().getEnvelopeversion()); | ||
SoapUtil.parseResponse(soapEnvelope, value.byteStream()); | ||
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn; | ||
Object obj = resultsRequestSOAP.getProperty(0); | ||
Log.e(TAG, "Soap2JsonResponseBodyConverter:" + obj.toString()); | ||
try { | ||
return (T) new Gson().getAdapter(TypeToken.get(clazz)).fromJson(obj.toString()); | ||
//return (T) obj.toString(); | ||
//return (T) new Persister().read(clazz, obj.toString()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} finally { | ||
value.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.