}
public void setDescription(String description) {
this.description = description;
}
}
}
}
下面在ApiService中添加接口。
/**
-
获取图像识别结果
-
@param accessToken 获取鉴权认证Token
-
@param url 网络图片Url
-
@return JsonObject
*/
@FormUrlEncoded
@POST(“/rest/2.0/image-classify/v2/advanced_general”)
@Headers(“Content-Type:application/x-www-form-urlencoded; charset=utf-8”)
Call getDiscernResult(@Field(“access_token”) String accessToken,
@Field(“url”) String url);
我这个写法和官方说明好像有点不同,我没有用Body。选择直接把数据放在Url中请求。
现在万事具备了,下面先来获取鉴权认证Token。
打开MainActivity,添加如下代码:
private static final String TAG = “MainActivity”;
/**
- Api服务
*/
private ApiService service;
/**
- 鉴权Toeken
*/
private String accessToken;
然后在onCreate方法中对ApiService进行实例化。
service = ServiceGenerator.createService(ApiService.class);
新增如下方法获取Token,
/**
- 访问API获取接口
*/
private void requestApiGetToken() {
String grantType = “client_credentials”;
String apiKey = “TjPChftoEyBq7Nzm65KNerqr”;
String apiSecret = “eTph4jO95te6R3G2aecktGMbkieOv7rS”;
service.getToken(grantType, apiKey, apiSecret)
.enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if (response.body() != null) {
//鉴权Token
accessToken = response.body().getAccess_token();
Log.d(TAG,accessToken);
}
}
@Override
public void onFailed(String errorStr) {
Log.e(TAG, “获取Token失败,失败原因:” + errorStr);
accessToken = null;
}
});
}
然后在onCreate中调用它。
运行一下,看一下控制台是否打印了日志。
你可以看到这个Token还是挺长的。对于这个Token,是有有效期的,基本上是一个月,所以你可以不用每次使用时都重新请求这个接口去获取Token,这里可以用缓存来解决这个问题。
说一下逻辑,当通过接口拿到Token时保存Token、Token获取时间、Token有效时长三个数据到缓存中,每一次使用前进行一次判断,首先是判断有没有Token,其次是判断Token有没有过期。那么按照这个思路我们就可以这么写代码了。
这里为了方便我在com.llw.imagediscerndemo包下新建一个util包,包下新建一个Constant类,里面的代码如下:
package com.llw.imagediscerndemo.util;
/**
- 全局常量
*/
public class Constant {
/**
- 鉴权Token
*/
public static final String TOKEN = “accessToken”;
/**
- 获取Token的时间
*/
public static final String GET_TOKEN_TIME = “getTokenTime”;
/**
- Token有效期
*/
public static final String TOKEN_VALID_PERIOD = “tokenValidPeriod”;
}
这三个值,我刚才也说明过了。下面写一个缓存的SPUtils工具类,里面的代码如下:
package com.llw.imagediscerndemo.util;
import android.content.Context;
import android.content.SharedPreferences;
/**
-
SharedPreferences工具类
-
@author llw
*/
public class SPUtils {
private static final String NAME = “config”;
public static void putBoolean(String key, boolean value, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(String key, boolean defValue, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
return sp.getBoolean(key, defValue);
}
public static void putString(String key, String value, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
sp.edit().putString(key, value).commit();
}
public static String getString(String key, String defValue, Context context) {
if (context != null) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
return sp.getString(key, defValue);
}
return “”;
}
public static void putInt(String key, int value, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
sp.edit().putInt(key, value).commit();
}
public static int getInt(String key, int defValue, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
return sp.getInt(key, defValue);
}
public static void putLong(String key, long value, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
sp.edit().putLong(key, value).commit();
}
public static long getLong(String key, long defValue, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
return sp.getLong(key, defValue);
}
public static void remove(String key, Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
sp.edit().remove(key).commit();
}
}
也是很简单的代码,相信你一眼就看明白了,下面就该在MainActivity中去处理缓存数据的存取了。
首先是放缓存,这当然是在请求接口的成功数据返回中放,修改onSuccess中的代码,如下。
@Override
public void onSuccess(Call call, Response response) {
if (response.body() != null) {
//鉴权Token
accessToken = response.body().getAccess_token();
//过期时间 秒
long expiresIn = response.body().getExpires_in();
//当前时间 秒
long currentTimeMillis = System.currentTimeMillis() / 1000;
//放入缓存
SPUtils.putString(Constant.TOKEN, accessToken, MainActivity.this);
SPUtils.putLong(Constant.GET_TOKEN_TIME, currentTimeMillis, MainActivity.this);
SPUtils.putLong(Constant.TOKEN_VALID_PERIOD, expiresIn, MainAc