├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_web.xml
│ │ │ │ ├── login_dialog_layout.xml
│ │ │ │ └── activity_index.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_refresh_black_24dp.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── menu
│ │ │ │ └── tools.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xiaoeshopsdk
│ │ │ │ └── sample
│ │ │ │ ├── net
│ │ │ │ ├── HttpLogger.java
│ │ │ │ ├── SimpleCallBack.java
│ │ │ │ ├── AccessTokenEntity.java
│ │ │ │ ├── LoginEntity.java
│ │ │ │ ├── BaseCallBack.java
│ │ │ │ └── OkHttpHelper.java
│ │ │ │ ├── SdkDemoApplication.java
│ │ │ │ ├── utils
│ │ │ │ └── Const.java
│ │ │ │ ├── IndexActivity.java
│ │ │ │ └── XeSdkDemoActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xiaoeshopsdk
│ │ │ └── sample
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xiaoeshopsdk
│ │ └── sample
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── .github
└── ISSUE_TEMPLATE
│ └── bug--.md
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaoeteam/XiaoeAppSDK-Android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | sample
3 | 登录成功
4 | 登录失败
5 | "用户名不能为空!"
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .idea
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Jun 30 23:52:53 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/HttpLogger.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | import android.util.Log;
4 | import okhttp3.logging.HttpLoggingInterceptor;
5 |
6 | public class HttpLogger implements HttpLoggingInterceptor.Logger {
7 | @Override
8 | public void log(String message) {
9 | Log.d("HttpLogInfo", message);
10 | }
11 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_web.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug--.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug报告
3 | about: 创建一个Bug报告帮助我们改善项目质量
4 | title: "【Bug】报告"
5 | labels: bug
6 | assignees: xiaoeteam
7 |
8 | ---
9 |
10 | **Bug描述**
11 | 清楚而简明地描述bug是什么。
12 |
13 | **重现步骤**
14 | 重现行为的步骤:
15 | xxxx
16 |
17 | **预期行为**
18 | 清楚而简明地描述你所期望发生的事情。
19 |
20 | **截图**
21 | 如果可以的话,添加屏幕截图来帮助解释您的问题。
22 |
23 | **手机(请填写以下信息):**
24 | - 设备类型: [e.g. iPhone6]
25 | - 设备系统版本: [e.g. iOS8.1]
26 | - SDK版本: [e.g. 1.0]
27 |
28 | **额外的信息**
29 | 在这里添加关于这个问题的任何其他信息。
30 |
--------------------------------------------------------------------------------
/app/src/test/java/com/xiaoeshopsdk/sample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_refresh_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/SdkDemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample;
2 |
3 | import android.app.Application;
4 | import com.xiaoe.shop.webcore.core.XiaoEWeb;
5 | import com.xiaoeshopsdk.sample.utils.Const;
6 |
7 | public class SdkDemoApplication extends Application{
8 |
9 | @Override
10 | public void onCreate(){
11 | super.onCreate();
12 | //为了使用的稳定性,建议此处指定使用X5内核的WebView
13 | XiaoEWeb.init(this, Const.APP_ID, Const.CLIENT_ID, XiaoEWeb.WebViewType.X5);
14 | XiaoEWeb.isOpenLog(true);
15 | }
16 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/tools.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/SimpleCallBack.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | import android.content.Context;
4 |
5 | import okhttp3.Request;
6 | import okhttp3.Response;
7 |
8 | public abstract class SimpleCallBack extends BaseCallBack
9 | {
10 | protected Context mContext;
11 | public SimpleCallBack(Context context)
12 | {
13 | mContext = context;
14 | }
15 |
16 | @Override
17 | public void onRequestBefore(Request request){}
18 |
19 | @Override
20 | public void onResponse(Response response){}
21 |
22 | @Override
23 | public void onTokenError(Response response, int code){}
24 | }
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | [](https://www.xiaoe-tech.com/)
5 |
6 | ## 简述
7 |
8 | 通过小鹅通 APP 内嵌 SDK 可以在 App 内快速集成小鹅通提供的整个交易服务,享受完善的基础知识商品能力、营销玩法,更有小鹅通强劲的技术及服务作保障,实现低成本、高效率、强融合的移动商城方案,快速获得 App 流量的商业化变现。
9 |
10 | ## 引入
11 |
12 | ```
13 | allprojects {
14 | repositories {
15 | mavenCentral()
16 | }
17 | }
18 | ```
19 |
20 | 在子项目build.gradle的dependencies中根据需求引入依赖:
21 | ```
22 | implementation 'com.xiaoetong.android.sdk:webcore:2.2.19'//最新版本号
23 | implementation 'com.tencent.tbs.tbssdk:sdk:43993'
24 | ```
25 | ## 文档
26 | [接入文档](https://github.com/xiaoeteam/XiaoeAppSDK-Android/wiki "接入文档")
27 |
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/xiaoeshopsdk/sample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.InstrumentationRegistry;
6 | import androidx.test.runner.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getTargetContext();
24 |
25 | assertEquals("com.xiaoeshopsdk.sample", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/utils/Const.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.utils;
2 |
3 | /**
4 | * Created by york on 2019-06-26.
5 | */
6 | public class Const {
7 |
8 | //Demo测试店铺的登录接口(三方 App 用户需要实现SDK的登录,具体请看文档)
9 | public static final String SDK_LOGIN_URL = "https://app38itOR341547.sdk.xiaoe-tech.com/sdk_api/xe.account.login.test/1.0.0";
10 | //Demo测试店铺的Url(三方 App 用户购买小鹅店铺sdk获取店铺Url 替换)
11 | public static final String SHOP_URL = "https://app38itOR341547.h5.xiaoeknow.com";
12 | //Demo测试店铺的appID(三方 App 用户注册使用小鹅店铺,取得店铺appID 替换)
13 | public static final String APP_ID = "app38itOR341547";
14 | //Demo测试店铺的sdkAppId(三方 App 用户购买小鹅店铺sdk获取sdkAppId 替换)
15 | public static final String CLIENT_ID = "883pzzGyzynE72G";
16 | //Demo测试店铺的秘钥(三方 App 用户购买小鹅店铺sdk获取的秘钥 替换)
17 | public static final String SECRET_KEY = "dfomGwT7JRWWnzY3okZ6yTkHtgNPTyhr";
18 |
19 | //下面两个接口不建议三方 App 用户直接在自己的app端进行调用,而是三方 App 用户后台写一个sdk登录的接口提供给自己的app使用,
20 | //当app请求后台的这个接口,由三方 App 用户的后台调用下面两个接口(我司的SDK登录接口),三方 App 用户的后台调用成功,把token返回给app
21 | public static final String GET_TOKEN_URL = "http://api.xiaoe-tech.com/token";
22 | public static final String LOGIN_URL = "http://api.xiaoe-tech.com/xe.sdk.account.login/1.0.0";
23 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/AccessTokenEntity.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | public class AccessTokenEntity {
4 |
5 | private int code;
6 | private String msg;
7 | private Data data;
8 |
9 | public int getCode() {
10 | return code;
11 | }
12 |
13 | public void setCode(int code) {
14 | this.code = code;
15 | }
16 |
17 | public String getMsg() {
18 | return msg;
19 | }
20 |
21 | public void setMsg(String msg) {
22 | this.msg = msg;
23 | }
24 |
25 | public Data getData() {
26 | return data;
27 | }
28 |
29 | public void setData(Data data) {
30 | this.data = data;
31 | }
32 |
33 | public static class Data{
34 | private String access_token;
35 | private int expires_in;
36 |
37 | public String getAccess_token() {
38 | return access_token;
39 | }
40 |
41 | public void setAccess_token(String access_token) {
42 | this.access_token = access_token;
43 | }
44 |
45 | public int getExpires_in() {
46 | return expires_in;
47 | }
48 |
49 | public void setExpires_in(int expires_in) {
50 | this.expires_in = expires_in;
51 | }
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "29.0.0"
6 | defaultConfig {
7 | applicationId "com.xiaoeshopsdk.sample"
8 | minSdkVersion 15
9 | targetSdkVersion 28
10 | versionCode 5
11 | versionName "1.0.5"
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
24 | implementation 'com.android.support:appcompat-v7:28.0.0'
25 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29 | implementation 'com.squareup.okhttp3:okhttp:4.2.2'
30 | implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'
31 | implementation 'com.google.code.gson:gson:2.8.5'
32 | implementation 'com.xiaoe.shop:webcore:2.2.19'
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/IndexActivity.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.view.Window;
7 | import android.widget.TextView;
8 | import androidx.annotation.Nullable;
9 | import androidx.appcompat.app.AppCompatActivity;
10 |
11 | import com.xiaoe.shop.webcore.core.XiaoEWeb;
12 |
13 | public class IndexActivity extends AppCompatActivity {
14 |
15 | @Override
16 | protected void onCreate(@Nullable Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
19 | setContentView(R.layout.activity_index);
20 |
21 | TextView logoutTv = findViewById(R.id.user_logout_tv);
22 | logoutTv.setOnClickListener(new View.OnClickListener() {
23 | @Override
24 | public void onClick(View v) {
25 | //在三方 App 内发生用户切换或用户退出的时, 为了避免出现客户信息混乱, 请务必执行如下代码登出小鹅通用户角色.
26 | XiaoEWeb.userLogout(IndexActivity.this.getApplicationContext());
27 | }
28 | });
29 |
30 | TextView testShopTv = findViewById(R.id.test_shop_tv);
31 | testShopTv.setOnClickListener(new View.OnClickListener() {
32 | @Override
33 | public void onClick(View v) {
34 | startActivity(new Intent(IndexActivity.this, XeSdkDemoActivity.class));
35 | }
36 | });
37 | }
38 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/login_dialog_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
21 |
22 |
26 |
27 |
33 |
34 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/LoginEntity.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | public class LoginEntity
4 | {
5 | /**
6 | * code : 0
7 | * msg : 成功 hint:[2482]
8 | * data : {"token_key":"xe_sdk_token","token_value":"d6a09cd4c0111e92e0dd5594234e336d"}
9 | */
10 |
11 | private int code;
12 | private String msg;
13 | private DataBean data;
14 |
15 | public int getCode() {
16 | return code;
17 | }
18 |
19 | public void setCode(int code) {
20 | this.code = code;
21 | }
22 |
23 | public String getMsg() {
24 | return msg;
25 | }
26 |
27 | public void setMsg(String msg) {
28 | this.msg = msg;
29 | }
30 |
31 | public DataBean getData() {
32 | return data;
33 | }
34 |
35 | public void setData(DataBean data) {
36 | this.data = data;
37 | }
38 |
39 | public static class DataBean {
40 | /**
41 | * token_key : xe_sdk_token
42 | * token_value : d6a09cd4c0111e92e0dd5594234e336d
43 | */
44 |
45 | private String token_key;
46 | private String token_value;
47 |
48 | public String getToken_key() {
49 | return token_key;
50 | }
51 |
52 | public void setToken_key(String token_key) {
53 | this.token_key = token_key;
54 | }
55 |
56 | public String getToken_value() {
57 | return token_value;
58 | }
59 |
60 | public void setToken_value(String token_value) {
61 | this.token_value = token_value;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/BaseCallBack.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | import com.google.gson.internal.$Gson$Types;
4 | import java.io.IOException;
5 | import java.lang.reflect.ParameterizedType;
6 | import java.lang.reflect.Type;
7 | import okhttp3.Request;
8 | import okhttp3.Response;
9 |
10 | public abstract class BaseCallBack
11 | {
12 | public Type mType;
13 |
14 | static Type getSuperclassTypeParameter(Class> subClass)
15 | {
16 | Type superclass = subClass.getGenericSuperclass();
17 | if (superclass instanceof Class)
18 | {
19 | throw new RuntimeException("Missing type parameter.");
20 | }
21 | ParameterizedType parameterized = (ParameterizedType) superclass;
22 | return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
23 | }
24 |
25 | public BaseCallBack()
26 | {
27 | mType = getSuperclassTypeParameter(getClass());
28 | }
29 |
30 | public abstract void onRequestBefore(Request request);
31 |
32 | public abstract void onFailure(Request request , IOException e);
33 |
34 | /**
35 | * 请求成功时调用此方法
36 | */
37 | public abstract void onResponse(Response response);
38 |
39 | /**
40 | * 状态码大于200,小于300时调用此方法
41 | */
42 | public abstract void onSuccess(Response response , T t);
43 |
44 | /**
45 | * 状态码400,403,500等时调用此方法
46 | */
47 | public abstract void onError(Response response , int code , Exception e);
48 |
49 | /**
50 | * Token验证失败,状态码401,402,403等时调用此方法
51 | */
52 | public abstract void onTokenError(Response response , int code);
53 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_index.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
21 |
22 |
29 |
30 |
31 |
32 |
36 |
37 |
45 |
46 |
50 |
51 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/XeSdkDemoActivity.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.text.TextUtils;
6 | import android.view.KeyEvent;
7 | import android.view.LayoutInflater;
8 | import android.view.Menu;
9 | import android.view.MenuInflater;
10 | import android.view.MenuItem;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.Button;
14 | import android.widget.EditText;
15 | import android.widget.RelativeLayout;
16 | import android.widget.Toast;
17 | import androidx.annotation.Nullable;
18 | import androidx.appcompat.app.ActionBar;
19 | import androidx.appcompat.app.AlertDialog;
20 | import androidx.appcompat.app.AppCompatActivity;
21 | import com.xiaoe.shop.webcore.core.XEToken;
22 | import com.xiaoe.shop.webcore.core.XiaoEWeb;
23 | import com.xiaoe.shop.webcore.core.bridge.JsBridgeListener;
24 | import com.xiaoe.shop.webcore.core.bridge.JsCallbackResponse;
25 | import com.xiaoe.shop.webcore.core.bridge.JsInteractType;
26 | import com.xiaoeshopsdk.sample.net.LoginEntity;
27 | import com.xiaoeshopsdk.sample.net.OkHttpHelper;
28 | import com.xiaoeshopsdk.sample.net.SimpleCallBack;
29 | import com.xiaoeshopsdk.sample.utils.Const;
30 | import java.io.IOException;
31 | import java.util.HashMap;
32 | import java.util.Map;
33 | import okhttp3.Request;
34 | import okhttp3.Response;
35 |
36 | public class XeSdkDemoActivity extends AppCompatActivity implements View.OnClickListener {
37 |
38 | private AlertDialog mLoginDlg;
39 | private Button mConfirmBtn, mCancelBtn;
40 | private EditText mUserNameEdit;
41 |
42 | private XiaoEWeb xiaoEWeb;
43 | private OkHttpHelper mOkHttpHelper;
44 |
45 | @Override
46 | protected void onCreate(Bundle savedInstanceState) {
47 | super.onCreate(savedInstanceState);
48 | setContentView(R.layout.activity_web);
49 | ActionBar actionBar = getSupportActionBar();
50 | if (actionBar != null) {
51 | actionBar.setDisplayHomeAsUpEnabled(true);
52 | actionBar.setDisplayShowTitleEnabled(true);
53 | actionBar.setDisplayShowHomeEnabled(true);
54 | actionBar.setDisplayShowCustomEnabled(true);
55 | }
56 |
57 | mOkHttpHelper = OkHttpHelper.getInstance();
58 | initView();
59 | initEvent();
60 | }
61 |
62 | @Override
63 | protected void onResume() {
64 | super.onResume();
65 | xiaoEWeb.onResume();
66 | }
67 |
68 | private void initView() {
69 | View loginLayout = LayoutInflater.from(XeSdkDemoActivity.this).inflate(R.layout.login_dialog_layout, null);
70 | mConfirmBtn = loginLayout.findViewById(R.id.confirm_login_btn);
71 | mCancelBtn = loginLayout.findViewById(R.id.cancel_login_btn);
72 | mUserNameEdit = loginLayout.findViewById(R.id.user_name_edit);
73 | mLoginDlg = new AlertDialog.Builder(XeSdkDemoActivity.this).setView(loginLayout).create();
74 | mLoginDlg.setCancelable(false);
75 |
76 | RelativeLayout webLayout = findViewById(R.id.web_layout);
77 | xiaoEWeb = XiaoEWeb.with(this)
78 | .setWebParent(webLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
79 | .useDefaultUI()
80 | .useDefaultTopIndicator(getResources().getColor(R.color.colorPrimaryDark))
81 | .buildWeb()
82 | .loadUrl(Const.SHOP_URL);
83 | }
84 |
85 | private void initEvent() {
86 | xiaoEWeb.setJsBridgeListener(new JsBridgeListener() {
87 | @Override
88 | public void onJsInteract(int actionType, JsCallbackResponse response) {
89 | switch (actionType) {
90 | case JsInteractType.LOGIN_ACTION:
91 | //H5登录态请求 这里三方 APP 应该调用登陆接口,获取到token_key, token_value
92 | //如果登录成功,通过XiaoEWeb.sync(XEToken)方法同步登录态到H5页面
93 | //如果登录失败,通过XiaoEWeb.syncNot()清除登录态(注意用户需要实现自己的SDK的登录)
94 | if (mLoginDlg != null && !mLoginDlg.isShowing())
95 | mLoginDlg.show();
96 | break;
97 |
98 | case JsInteractType.SHARE_ACTION:
99 | //H5分享请求回调,通过 response.getResponseData() 获取分享的数据,这里三方 APP 自行分享登录方法
100 | Toast.makeText(XeSdkDemoActivity.this, response.getResponseData(), Toast.LENGTH_SHORT).show();
101 | break;
102 |
103 | case JsInteractType.TITLE_RECEIVE:
104 | //H5标题回调,通过 response.getResponseData() 获取标题
105 | break;
106 |
107 | case JsInteractType.NOTICE_OUT_LINK_ACTION:
108 | //sdk通知需要外部打开的链接回调,通过 response.getResponseData() 获取外链
109 | //原来后台自定义链接:
110 | //1.本来不带参数的 http://www.baidu.com
111 | //2.本来带参数的 http://www.baidu.com?xxx=xxx
112 | //需要sdk通知外部打开,拼装带参数needoutlink=1
113 | //1.本来不带参数的 http://www.baidu.com?needoutlink=1
114 | //2.本来带参数的 http://www.baidu.com?xxx=xxx&needoutlink=1
115 | break;
116 | }
117 | }
118 | });
119 | mConfirmBtn.setOnClickListener(this);
120 | mCancelBtn.setOnClickListener(this);
121 | }
122 |
123 | @Override
124 | public void onClick(View v) {
125 | switch (v.getId()) {
126 | case R.id.confirm_login_btn:
127 | if (!TextUtils.isEmpty(mUserNameEdit.getText())) {
128 | doLogin(mUserNameEdit.getText().toString());
129 | } else {
130 | Toast.makeText(XeSdkDemoActivity.this, R.string.user_name_empty_tip, Toast.LENGTH_SHORT).show();
131 | }
132 | break;
133 |
134 | case R.id.cancel_login_btn:
135 | if (mLoginDlg != null && mLoginDlg.isShowing()) {
136 | mLoginDlg.dismiss();
137 | xiaoEWeb.loginCancel();
138 | }
139 | break;
140 | }
141 | }
142 |
143 | @Override
144 | public boolean onCreateOptionsMenu(Menu menu) {
145 | MenuInflater menuInflater = getMenuInflater();
146 | menuInflater.inflate(R.menu.tools, menu);
147 | return super.onCreateOptionsMenu(menu);
148 | }
149 |
150 | @Override
151 | public boolean onOptionsItemSelected(MenuItem item){
152 | switch (item.getItemId()){
153 | case android.R.id.home:
154 | finish();
155 | return true;
156 | case R.id.share:
157 | xiaoEWeb.share();
158 | return true;
159 | case R.id.logout:
160 | xiaoEWeb.syncNot();
161 | return true;
162 | case R.id.fresh:
163 | if (xiaoEWeb != null)
164 | xiaoEWeb.reload();
165 | return true;
166 | default:
167 | return super.onOptionsItemSelected(item);
168 | }
169 | }
170 |
171 | @Override
172 | protected void onPause() {
173 | super.onPause();
174 | xiaoEWeb.onPause();
175 | }
176 |
177 | @Override
178 | protected void onDestroy() {
179 | super.onDestroy();
180 | xiaoEWeb.onDestroy();
181 | }
182 |
183 | @Override
184 | public boolean onKeyDown(int keyCode, KeyEvent event) {
185 | if (xiaoEWeb.handlerKeyEvent(keyCode, event))
186 | return true;
187 | return super.onKeyDown(keyCode, event);
188 | }
189 |
190 | @Override
191 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
192 | super.onActivityResult(requestCode, resultCode, data);
193 | xiaoEWeb.onActivityResult(requestCode, resultCode, data);
194 | }
195 |
196 | private void doLogin(String userName){
197 | Map params = new HashMap<>(4);
198 | params.put("user_id", userName);
199 | params.put("app_user_id", userName);
200 | params.put("secret_key", Const.SECRET_KEY);
201 | params.put("sdk_app_id", Const.CLIENT_ID);
202 | params.put("app_id", Const.APP_ID);
203 | mOkHttpHelper.post(Const.SDK_LOGIN_URL, params, new SimpleCallBack(XeSdkDemoActivity.this) {
204 | @Override
205 | public void onFailure(Request request, IOException e) {
206 | mLoginDlg.dismiss();
207 | Toast.makeText(XeSdkDemoActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
208 | }
209 |
210 | @Override
211 | public void onSuccess(Response response, LoginEntity loginEntity) {
212 | mLoginDlg.dismiss();
213 | if (loginEntity != null && loginEntity.getCode() == 0 && loginEntity.getData() != null) {
214 | Toast.makeText(XeSdkDemoActivity.this, R.string.login_success, Toast.LENGTH_SHORT).show();
215 | XEToken token = new XEToken(loginEntity.getData().getToken_key(), loginEntity.getData().getToken_value());
216 | xiaoEWeb.sync(token);
217 | } else {
218 | Toast.makeText(XeSdkDemoActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
219 | }
220 | }
221 |
222 | @Override
223 | public void onError(Response response, int code, Exception e) {
224 | mLoginDlg.dismiss();
225 | Toast.makeText(XeSdkDemoActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
226 | }
227 | });
228 | }
229 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/xiaoeshopsdk/sample/net/OkHttpHelper.java:
--------------------------------------------------------------------------------
1 | package com.xiaoeshopsdk.sample.net;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 | import android.util.Log;
6 | import androidx.annotation.NonNull;
7 | import com.google.gson.Gson;
8 | import com.google.gson.JsonParseException;
9 | import java.io.IOException;
10 | import java.util.Map;
11 | import java.util.concurrent.TimeUnit;
12 | import okhttp3.Call;
13 | import okhttp3.Callback;
14 | import okhttp3.FormBody;
15 | import okhttp3.MediaType;
16 | import okhttp3.OkHttpClient;
17 | import okhttp3.Request;
18 | import okhttp3.RequestBody;
19 | import okhttp3.Response;
20 | import okhttp3.logging.HttpLoggingInterceptor;
21 |
22 | public class OkHttpHelper {
23 | public static final int TOKEN_MISSING = 401; //token¶ªÊ§
24 | public static final int TOKEN_ERROR = 402; //token´íÎó
25 | public static final int TOKEN_EXPIRE = 403; //token¹ýÆÚ
26 |
27 | private static OkHttpClient okHttpClient;
28 | private Handler handler;
29 | private Gson gson;
30 |
31 | private OkHttpHelper() {
32 | HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
33 | logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
34 | okHttpClient = new OkHttpClient.Builder()
35 | .readTimeout(10, TimeUnit.SECONDS)
36 | .writeTimeout(10, TimeUnit.SECONDS)
37 | .connectTimeout(10, TimeUnit.SECONDS)
38 | .addNetworkInterceptor(logInterceptor)
39 | .build();
40 | gson = new Gson();
41 | handler = new Handler(Looper.getMainLooper());
42 | }
43 |
44 | public static OkHttpHelper getInstance() {
45 | return new OkHttpHelper();
46 | }
47 |
48 | public void get(String url, BaseCallBack callBack) {
49 | get(url, null, callBack);
50 | }
51 |
52 | public void get(String url, Map params, BaseCallBack callBack) {
53 | Request request = buildGetRequest(url, params);
54 | doRequest(request, callBack);
55 | }
56 |
57 | public void post(String url, Map params, BaseCallBack callBack) {
58 | post(url, params, null, callBack);
59 | }
60 |
61 | public void post(String url, Map params, Map headers, BaseCallBack callBack) {
62 | Request request = buildPostRequest(url, params, headers);
63 | doRequest(request, callBack);
64 | }
65 |
66 | public Response doGet(String url, Map params, Map headers) throws IOException {
67 | return doRequest(buildGetRequest(url, params, headers));
68 | }
69 |
70 | public Response doPost(String url, Map params, Map headers) throws IOException {
71 | return doRequest(buildPostRequest(url, params, headers));
72 | }
73 |
74 | public Response doPost(String url, String body, Map headers) throws IOException {
75 | return doRequest(buildPostRequest(url, body, headers));
76 | }
77 |
78 | private Response doRequest(final Request request) throws IOException {
79 | return okHttpClient.newCall(request).execute();
80 | }
81 |
82 | public void doRequest(final Request request, final BaseCallBack callBack) {
83 | callBack.onRequestBefore(request);
84 |
85 | okHttpClient.newCall(request).enqueue(new Callback() {
86 | @Override
87 | public void onFailure(@NonNull Call call, @NonNull IOException e) {
88 | callbackFailed(callBack, call.request(), e);
89 | }
90 |
91 | @Override
92 | public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
93 | callbackResponse(callBack, response);
94 | if (response.isSuccessful()) {
95 | String resultStr = response.body().string();
96 | Log.d("okhttpresult11", "the result = " + resultStr);
97 | if (callBack.mType == String.class) {
98 | callbackSuccess(callBack, response, resultStr);
99 | } else {
100 | try {
101 | Object object = gson.fromJson(resultStr, callBack.mType);
102 | callbackSuccess(callBack, response, object);
103 | } catch (JsonParseException e) {
104 | callbackError(callBack, response, response.code(), e);
105 | }
106 | }
107 | } else if (response.code() == TOKEN_MISSING || response.code() == TOKEN_ERROR || response.code() == TOKEN_EXPIRE) {
108 | callbackTokenError(callBack, response);
109 | } else {
110 | callbackError(callBack, response, response.code(), null);
111 | }
112 | }
113 | });
114 | }
115 |
116 | private void callbackResponse(final BaseCallBack callBack, final Response response) {
117 | handler.post(new Runnable() {
118 | @Override
119 | public void run() {
120 | callBack.onResponse(response);
121 | }
122 | });
123 | }
124 |
125 | private void callbackFailed(final BaseCallBack callBack, final Request request, final IOException e) {
126 | handler.post(new Runnable() {
127 | @Override
128 | public void run() {
129 | callBack.onFailure(request, e);
130 | }
131 | });
132 | }
133 |
134 | private void callbackSuccess(final BaseCallBack callBack, final Response response, final Object object) {
135 | handler.post(new Runnable() {
136 | @Override
137 | public void run() {
138 | callBack.onSuccess(response, object);
139 | }
140 | });
141 | }
142 |
143 | private void callbackTokenError(final BaseCallBack callBack, final Response response) {
144 | handler.post(new Runnable() {
145 | @Override
146 | public void run() {
147 | callBack.onTokenError(response, response.code());
148 | }
149 | });
150 | }
151 |
152 | private void callbackError(final BaseCallBack callBack, final Response response, final int code, final Exception e) {
153 | handler.post(new Runnable() {
154 | @Override
155 | public void run() {
156 | callBack.onError(response, code, e);
157 | }
158 | });
159 | }
160 |
161 | private Request buildPostRequest(String url, Map params) {
162 | return buildPostRequest(url, params, null);
163 | }
164 |
165 | private Request buildPostRequest(String url, Map params, Map headers) {
166 | return buildRequest(url, params, headers, HttpMethodType.POST);
167 | }
168 |
169 | private Request buildPostRequest(String url, String body, Map headers) {
170 | Request.Builder builder = new Request.Builder().url(url);
171 | RequestBody requestBody = buildRequestBody(headers, body);
172 | builder.post(requestBody);
173 | if (headers != null) {
174 | for (Map.Entry header : headers.entrySet()) {
175 | builder.header(header.getKey(), header.getValue());
176 | }
177 | }
178 | return builder.build();
179 | }
180 |
181 | private Request buildGetRequest(String url, Map params) {
182 | return buildGetRequest(url, params, null);
183 | }
184 |
185 | private Request buildGetRequest(String url, Map params, Map headers) {
186 | return buildRequest(url, params, headers, HttpMethodType.GET);
187 | }
188 |
189 | private Request buildRequest(String url, Map params, Map headers, HttpMethodType methodType) {
190 | Request.Builder builder = new Request.Builder().url(url);
191 | if (methodType == HttpMethodType.GET) {
192 | url = buildUrlParams(url, params);
193 | builder.url(url);
194 | builder.get();
195 | } else if (methodType == HttpMethodType.POST) {
196 | RequestBody body = buildFormData(params);
197 | builder.post(body);
198 | }
199 | if (headers != null) {
200 | for (Map.Entry header : headers.entrySet()) {
201 | builder.header(header.getKey(), header.getValue());
202 | }
203 | }
204 | return builder.build();
205 | }
206 |
207 | private String buildUrlParams(String url, Map params) {
208 | if (params == null || params.isEmpty()) {
209 | return url;
210 | }
211 |
212 | StringBuilder sb = new StringBuilder();
213 | for (Map.Entry entry : params.entrySet()) {
214 | sb.append(entry.getKey()).append("=").append(entry.getValue() == null ? "" : entry.getValue().toString());
215 | sb.append("&");
216 | }
217 | String s = sb.toString();
218 | if (s.endsWith("&")) {
219 | s = s.substring(0, s.length() - 1);
220 | }
221 | if (url.indexOf("?") > 0) {
222 | url = url + "&" + s;
223 | } else {
224 | url = url + "?" + s;
225 | }
226 | return url;
227 | }
228 |
229 | private RequestBody buildFormData(Map params) {
230 | FormBody.Builder builder = new FormBody.Builder();
231 | if (params != null) {
232 | for (Map.Entry entry : params.entrySet()) {
233 | builder.add(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString());
234 | }
235 | }
236 | return builder.build();
237 | }
238 |
239 | private RequestBody buildRequestBody(Map headers, String body) {
240 | String contentType = null;
241 | if (headers != null) {
242 | contentType = headers.get("Content-Type");
243 | }
244 | if (contentType == null) {
245 | contentType = "application/json;charset=UTF-8";
246 | }
247 | return RequestBody.create(MediaType.parse(contentType), body);
248 | }
249 |
250 | private enum HttpMethodType {
251 | GET, POST
252 | }
253 | }
--------------------------------------------------------------------------------