Skip to content

Commit

Permalink
1,精简依赖 2,分为2部分:ksoap转换类,retrofit工具类。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangkezhi committed May 22, 2017
1 parent 4d004ff commit 42e2da5
Show file tree
Hide file tree
Showing 31 changed files with 606 additions and 1,095 deletions.
21 changes: 21 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 33 additions & 24 deletions README.md
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.


2 changes: 1 addition & 1 deletion app/src/main/java/com/expert/newpams/TestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

private void testapi() {

//initdownloadbasicdata();
initdownloadbasicdata();
initdownloadplandata();
}

Expand Down
1 change: 1 addition & 0 deletions ksoap2retrofit-convertfactory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions ksoap2retrofit-convertfactory/build.gradle
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'
}
25 changes: 25 additions & 0 deletions ksoap2retrofit-convertfactory/proguard-rules.pro
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
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 ksoap2retrofit-convertfactory/src/main/AndroidManifest.xml
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>
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>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ximencx.ksoap2retrofit.convert;
package com.ximencx.ksoap2retrofit_convertfactory.convertfactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
Expand All @@ -15,18 +15,18 @@
* summary:自定义转换器
*/

public class SoapConverterFactory<T> extends Converter.Factory {
public class KSoap2XmlConverterFactory<T> extends Converter.Factory {


public static SoapConverterFactory create() {
return new SoapConverterFactory();
public static KSoap2XmlConverterFactory create() {
return new KSoap2XmlConverterFactory();
}


@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return new SoapResponseBodyConverter<Type>(type);
return new Soap2XmlResponseBodyConverter<Type>(type);
}


Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ximencx.ksoap2retrofit.convert;
package com.ximencx.ksoap2retrofit_convertfactory.convertfactory;

import android.util.Log;

import com.ximencx.ksoap2retrofit.SoapUtil;
import com.ximencx.ksoap2retrofit.ksoap2.SoapEnvelope;
import com.ximencx.ksoap2retrofit.ksoap2.serialization.SoapObject;
import com.ximencx.ksoap2retrofit.ksoap2.transport.SoapHelper;
import com.ximencx.ksoap2retrofit.ksoap2.serialization.SoapSerializationEnvelope;
import com.ximencx.ksoap2retrofit.KSoap2RetrofitHelper;

import org.simpleframework.xml.core.Persister;

Expand All @@ -23,22 +25,23 @@
* 功能描述:
* 修改:无
*/
public final class SoapResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private static final String TAG = "SoapResponseBodyConvert";
public final class Soap2XmlResponseBodyConverter<T> implements Converter<ResponseBody, T> {

private String TAG = getClass().getSimpleName();

private Class<?> clazz;

protected SoapResponseBodyConverter(Type clazz) {
protected Soap2XmlResponseBodyConverter(Type clazz) {
this.clazz = (Class<?>) clazz;
}

@Override
public T convert(ResponseBody value) throws IOException {
//value 参考soap的返回,截取其中的字符串json
SoapEnvelope soapEnvelope = SoapHelper.getInstance().getSoapEnvelope();
SoapHelper.getInstance().parseResponse(soapEnvelope, value.byteStream());
SoapEnvelope soapEnvelope = new SoapSerializationEnvelope(KSoap2RetrofitHelper.getInstance().getEnvelopeversion());
SoapUtil.parseResponse(soapEnvelope, value.byteStream());
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Object obj = (Object) resultsRequestSOAP.getProperty(0);
Object obj = resultsRequestSOAP.getProperty(0);
Log.e(TAG, "ResponseBodyToStringConverterFactory : " + obj.toString());
try {
//return adapter.fromJson(obj.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ximencx.ksoap2retrofit.convert;
package com.ximencx.ksoap2retrofit_convertfactory.convertfactory;

import android.util.Log;

Expand All @@ -18,12 +18,13 @@
* 修改:无
*/
final class SoapRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final String TAG = "SoapRequestBodyConverte";

private String TAG = getClass().getSimpleName();

private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain; charset=UTF-8");

@Override public RequestBody convert(T value) throws IOException {
Log.v(TAG,"RequestBody:"+(String)value);
Log.v(TAG,"RequestBody:"+value);
return RequestBody.create(MEDIA_TYPE, (String)value);
}
}
Loading

0 comments on commit 42e2da5

Please sign in to comment.