updated web-api using callbacks

This commit is contained in:
2017-07-02 08:26:45 +08:00
parent 979e7cf96a
commit 850c267aa4
5 changed files with 557 additions and 110 deletions

View File

@@ -0,0 +1,90 @@
package net.irext.webapi;
import net.irext.webapi.model.Brand;
import net.irext.webapi.model.Category;
import net.irext.webapi.model.City;
import net.irext.webapi.model.RemoteIndex;
import net.irext.webapi.model.StbOperator;
import net.irext.webapi.model.UserApp;
import java.io.InputStream;
import java.util.List;
/**
* Filename: WebAPICallbacks.java
* Revised: Date: 2017-07-01
* Revision: Revision: 1.0
* <p>
* Description: HTTP Response Callbacks
* <p>
* Revision log:
* 2017-07-01: created by strawmanbobi
*/
public class WebAPICallbacks {
public interface SignInCallback {
void onSignInSuccess(UserApp admin);
void onSignInFailed();
void onSignInError();
}
public interface ListCategoriesCallback {
void onListCategoriesSuccess(List<Category> categories);
void onListCategoriesFailed();
void onListCategoriesError();
}
public interface ListBrandsCallback {
void onListBrandsSuccess(List<Brand> brands);
void onListBrandsFailed();
void onListBrandsError();
}
public interface ListPopularBrandsCallback {
void onListPopularBrandsSuccess(List<Brand> brands);
void onListPopularBrandsFailed();
void onListPopularBrandsError();
}
public interface ListPopularCitiesCallback {
void onListPopularCitiesSuccess(List<City> cities);
void onListPopularCitiesFailed();
void onListPopularCitiesError();
}
public interface ListProvincesCallback {
void onListProvincesSuccess(List<City> provinces);
void onListProvincesFailed();
void onListProvincesError();
}
public interface ListCitiesCallback {
void onListCitiesSuccess(List<City> cities);
void onListCitiesFailed();
void onListCitiesError();
}
public interface ListAreasCallback {
void onListAreasSuccess(List<City> cities);
void onListAreasFailed();
void onListAreasError();
}
public interface ListOperatersCallback {
void onListOperatorsSuccess(List<StbOperator> operators);
void onListOperatorsFailed();
void onListOperatorsError();
}
public interface ListIndexesCallback {
void onListIndexesSuccess(List<RemoteIndex> indexes);
void onListIndexesFailed();
void onListIndexesError();
}
public interface DownloadBinCallback {
void onDownloadBinSuccess(InputStream inputStream);
void onDownloadBinFailed();
void onDownloadBinError();
}
}

View File

@@ -5,11 +5,11 @@ import net.irext.webapi.model.*;
import net.irext.webapi.utils.Constants;
import net.irext.webapi.request.*;
import net.irext.webapi.response.*;
import net.irext.webapi.WebAPICallbacks.*;
import okhttp3.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Filename: WebAPIs.java
@@ -94,7 +94,7 @@ public class WebAPIs {
}
@SuppressWarnings("unused")
public Admin signIn(String appKey, String appSecret) {
public void signIn(String appKey, String appSecret, SignInCallback signInCallback) {
String signInURL = URL_PREFIX + SERVICE_SIGN_IN;
AppSignInRequest appSignInRequest = new AppSignInRequest();
appSignInRequest.setAppKey(appKey);
@@ -106,21 +106,23 @@ public class WebAPIs {
String response = postToServer(signInURL, bodyJson);
LoginResponse loginResponse = new Gson().fromJson(response, LoginResponse.class);
if(loginResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
Admin admin = loginResponse.getEntity();
UserApp admin = loginResponse.getEntity();
if (0 != admin.getId() && null != admin.getToken()) {
adminId = admin.getId();
token = admin.getToken();
signInCallback.onSignInSuccess(admin);
} else {
signInCallback.onSignInFailed();
}
return admin;
}
} catch (Exception e) {
e.printStackTrace();
signInCallback.onSignInError();
}
return null;
}
@SuppressWarnings("unused")
public List<Category> listCategories(int from, int count) {
public void listCategories(int from, int count, ListCategoriesCallback listCategoriesCallback) {
String listCategoriesURL = URL_PREFIX + SERVICE_LIST_CATEGORIES;
ListCategoriesRequest listCategoriesRequest = new ListCategoriesRequest();
listCategoriesRequest.setAdminId(adminId);
@@ -134,16 +136,19 @@ public class WebAPIs {
CategoriesResponse categoriesResponse = new Gson().fromJson(response, CategoriesResponse.class);
if(categoriesResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
return categoriesResponse.getEntity();
listCategoriesCallback.onListCategoriesSuccess(categoriesResponse.getEntity());
} else {
listCategoriesCallback.onListCategoriesFailed();
}
} catch (Exception e) {
e.printStackTrace();
listCategoriesCallback.onListCategoriesError();
}
return null;
}
@SuppressWarnings("unused")
public List<Brand> listBrands(int categoryId, int from, int count) {
public void listBrands(int categoryId, int from, int count,
ListBrandsCallback listBrandsCallback) {
String listBrandsURL = URL_PREFIX + SERVICE_LIST_BRANDS;
ListBrandsRequest listBrandsRequest = new ListBrandsRequest();
listBrandsRequest.setAdminId(adminId);
@@ -157,28 +162,42 @@ public class WebAPIs {
String response = postToServer(listBrandsURL, bodyJson);
BrandsResponse brandsResponse = new Gson().fromJson(response, BrandsResponse.class);
if(brandsResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
return brandsResponse.getEntity();
if (brandsResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
listBrandsCallback.onListBrandsSuccess(brandsResponse.getEntity());
} else {
listBrandsCallback.onListBrandsFailed();
}
} catch (Exception e) {
e.printStackTrace();
listBrandsCallback.onListBrandsError();
}
return null;
}
@SuppressWarnings("unused")
public List<City> listProvinces() {
public void listProvinces(ListProvincesCallback listProvincesCallback) {
String listProvincesURL = URL_PREFIX + SERVICE_LIST_PROVINCES;
ListCitiesRequest listCitiesRequest = new ListCitiesRequest();
listCitiesRequest.setAdminId(adminId);
listCitiesRequest.setToken(token);
String bodyJson = listCitiesRequest.toJson();
return listCitiesCommon(listProvincesURL, bodyJson);
try {
String response = postToServer(listProvincesURL, bodyJson);
CitiesResponse citiesResponse = new Gson().fromJson(response, CitiesResponse.class);
if (citiesResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
listProvincesCallback.onListProvincesSuccess(citiesResponse.getEntity());
} else {
listProvincesCallback.onListProvincesFailed();
}
} catch (Exception e) {
e.printStackTrace();
listProvincesCallback.onListProvincesError();
}
}
@SuppressWarnings("unused")
public List<City> listCities(String prefix) {
public void listCities(String prefix, ListCitiesCallback listCitiesCallback) {
String listCitiesURL = URL_PREFIX + SERVICE_LIST_CITIES;
ListCitiesRequest listCitiesRequest = new ListCitiesRequest();
listCitiesRequest.setAdminId(adminId);
@@ -186,25 +205,24 @@ public class WebAPIs {
listCitiesRequest.setProvincePrefix(prefix);
String bodyJson = listCitiesRequest.toJson();
return listCitiesCommon(listCitiesURL, bodyJson);
}
private List<City> listCitiesCommon(String url, String bodyJson) {
try {
String response = postToServer(url, bodyJson);
String response = postToServer(listCitiesURL, bodyJson);
CitiesResponse citiesResponse = new Gson().fromJson(response, CitiesResponse.class);
if (citiesResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
return citiesResponse.getEntity();
listCitiesCallback.onListCitiesSuccess(citiesResponse.getEntity());
} else {
listCitiesCallback.onListCitiesFailed();
}
} catch (Exception e) {
e.printStackTrace();
listCitiesCallback.onListCitiesError();
}
return null;
}
@SuppressWarnings("unused")
public List<StbOperator> listOperators(String cityCode) {
public void listOperators(String cityCode,
ListOperatersCallback listOperatersCallback) {
String listOperatorsURL = URL_PREFIX + SERVICE_LIST_OPERATORS;
ListOperatorsRequest listOperatorsRequest = new ListOperatorsRequest();
listOperatorsRequest.setAdminId(adminId);
@@ -219,16 +237,22 @@ public class WebAPIs {
OperatorsResponse operatorsResponse = new Gson().fromJson(response, OperatorsResponse.class);
if (operatorsResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
return operatorsResponse.getEntity();
listOperatersCallback.onListOperatorsSuccess(operatorsResponse.getEntity());
} else {
listOperatersCallback.onListOperatorsFailed();
}
} catch (Exception e) {
e.printStackTrace();
listOperatersCallback.onListOperatorsError();
}
return null;
}
@SuppressWarnings("unused")
public List<RemoteIndex> listRemoteIndexes(int categoryId, int brandId, String cityCode, String operatorId) {
public void listRemoteIndexes(int categoryId,
int brandId,
String cityCode,
String operatorId,
ListIndexesCallback onListIndexCallback) {
String listIndexesURL = URL_PREFIX + SERVICE_LIST_INDEXES;
ListIndexesRequest listIndexesRequest = new ListIndexesRequest();
listIndexesRequest.setAdminId(adminId);
@@ -247,16 +271,19 @@ public class WebAPIs {
IndexesResponse indexesResponse = new Gson().fromJson(response, IndexesResponse.class);
if (indexesResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
return indexesResponse.getEntity();
onListIndexCallback.onListIndexesSuccess(indexesResponse.getEntity());
} else {
onListIndexCallback.onListIndexesFailed();
}
} catch (Exception e) {
e.printStackTrace();
onListIndexCallback.onListIndexesError();
}
return null;
}
@SuppressWarnings("unused")
public InputStream downloadBin(String remoteMap, int indexId) {
public void downloadBin(String remoteMap, int indexId,
DownloadBinCallback downloadBinCallback) {
String downloadURL = URL_PREFIX + SERVICE_DOWNLOAD_BIN;
DownloadBinaryRequest downloadBinaryRequest = new DownloadBinaryRequest();
downloadBinaryRequest.setAdminId(adminId);
@@ -267,15 +294,22 @@ public class WebAPIs {
if (null != bodyJson) {
try {
return postToServerForOctets(downloadURL, bodyJson);
InputStream binStream = postToServerForOctets(downloadURL, bodyJson);
if (null != binStream) {
downloadBinCallback.onDownloadBinSuccess(binStream);
} else {
downloadBinCallback.onDownloadBinFailed();
}
} catch (IOException e) {
e.printStackTrace();
downloadBinCallback.onDownloadBinError();
}
}
return null;
}
@SuppressWarnings("unused")
@Deprecated
public int[] decodeIR(int indexId) {
String decodeURL = URL_PREFIX + SERVICE_ONLINE_DECODE;
DecodeRequest decodeRequest = new DecodeRequest();

View File

@@ -1,73 +0,0 @@
package net.irext.webapi.model;
/**
* Filename: Admin.java
* Revised: Date: 2017-04-01
* Revision: Revision: 1.0
* <p>
* Description: Admin bean
* <p>
* Revision log:
* 2017-04-01: created by strawmanbobi
*/
public class Admin {
private int id;
private String userName;
private String password;
private String token;
private String permissions;
private int adminType;
public Admin() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getPermissions() {
return permissions;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
public int getAdminType() {
return adminType;
}
public void setAdminType(int adminType) {
this.adminType = adminType;
}
}

View File

@@ -0,0 +1,397 @@
package net.irext.webapi.model;
public class UserApp {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private Integer id;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.app_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String appName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.admin_id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private Integer adminId;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.admin_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String adminName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.app_type
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private Byte appType;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.android_package_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String androidPackageName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.android_signature
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String androidSignature;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.ios_identity
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String iosIdentity;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.is_debug
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private Byte isDebug;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.app_key
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String appKey;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.app_secret
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String appSecret;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_app.update_time
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
private String updateTime;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.id
*
* @return the value of user_app.id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.id
*
* @param id the value for user_app.id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.app_name
*
* @return the value of user_app.app_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAppName() {
return appName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.app_name
*
* @param appName the value for user_app.app_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAppName(String appName) {
this.appName = appName;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.admin_id
*
* @return the value of user_app.admin_id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public Integer getAdminId() {
return adminId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.admin_id
*
* @param adminId the value for user_app.admin_id
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAdminId(Integer adminId) {
this.adminId = adminId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.admin_name
*
* @return the value of user_app.admin_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAdminName() {
return adminName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.admin_name
*
* @param adminName the value for user_app.admin_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAdminName(String adminName) {
this.adminName = adminName;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.app_type
*
* @return the value of user_app.app_type
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public Byte getAppType() {
return appType;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.app_type
*
* @param appType the value for user_app.app_type
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAppType(Byte appType) {
this.appType = appType;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.android_package_name
*
* @return the value of user_app.android_package_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAndroidPackageName() {
return androidPackageName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.android_package_name
*
* @param androidPackageName the value for user_app.android_package_name
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAndroidPackageName(String androidPackageName) {
this.androidPackageName = androidPackageName;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.android_signature
*
* @return the value of user_app.android_signature
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAndroidSignature() {
return androidSignature;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.android_signature
*
* @param androidSignature the value for user_app.android_signature
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAndroidSignature(String androidSignature) {
this.androidSignature = androidSignature;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.ios_identity
*
* @return the value of user_app.ios_identity
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getIosIdentity() {
return iosIdentity;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.ios_identity
*
* @param iosIdentity the value for user_app.ios_identity
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setIosIdentity(String iosIdentity) {
this.iosIdentity = iosIdentity;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.is_debug
*
* @return the value of user_app.is_debug
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public Byte getIsDebug() {
return isDebug;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.is_debug
*
* @param isDebug the value for user_app.is_debug
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setIsDebug(Byte isDebug) {
this.isDebug = isDebug;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.app_key
*
* @return the value of user_app.app_key
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAppKey() {
return appKey;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.app_key
*
* @param appKey the value for user_app.app_key
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAppKey(String appKey) {
this.appKey = appKey;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.app_secret
*
* @return the value of user_app.app_secret
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getAppSecret() {
return appSecret;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.app_secret
*
* @param appSecret the value for user_app.app_secret
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_app.update_time
*
* @return the value of user_app.update_time
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public String getUpdateTime() {
return updateTime;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_app.update_time
*
* @param updateTime the value for user_app.update_time
*
* @mbggenerated Fri May 26 19:47:48 CST 2017
*/
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

View File

@@ -1,7 +1,6 @@
package net.irext.webapi.response;
import net.irext.webapi.model.Admin;
import net.irext.webapi.model.UserApp;
/**
* Filename: LoginResponse.java
* Revised: Date: 2017-03-31
@@ -14,9 +13,9 @@ import net.irext.webapi.model.Admin;
*/
public class LoginResponse extends ServiceResponse {
private Admin entity;
private UserApp entity;
public LoginResponse(Status status, Admin admin) {
public LoginResponse(Status status, UserApp admin) {
super(status);
this.entity = admin;
}
@@ -25,11 +24,11 @@ public class LoginResponse extends ServiceResponse {
}
public Admin getEntity() {
public UserApp getEntity() {
return entity;
}
public void setEntity(Admin entity) {
public void setEntity(UserApp entity) {
this.entity = entity;
}
}