我的项目中有以下3个类和1个接口.将日志记录设置为NONE会导致“成功”,但会有一个空的响应正文(见下面的getMovies() – > success()).可以删除在该错误中输出的URL&粘贴到浏览器中,它完美地运行.如果我将日志记录更改为FULL,则一切正常,但是,我的API密钥在LogCat输出中明显可见.
public class RestClient {
private ApiService apiService;
public RestClient(String endpoint, boolean enableLogging) {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory())
.setDateFormat("yyyy-MM-dd")
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(BuildConfig.DEBUG && enableLogging ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.setEndpoint(endpoint)
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(new SessionRequestInterceptor())
.build();
apiService = restAdapter.create(ApiService.class);
}
public ApiService getApiService() {
return apiService;
}
}
public class ServiceEventHandler {
private boolean isRetrofitLoggingEnabled;
private String rottenTomatoesUrl;
private String rottenTomatoesApiKey;
public ServiceEventHandler(Context context) {
isRetrofitLoggingEnabled = Boolean.valueOf(context.getString(R.string.retrofit_logging_enabled));
rottenTomatoesUrl = context.getString(R.string.movies_url);
rottenTomatoesApiKey = context.getString(R.string.rotten_tomatoes_api_key); /* Look in res/values/secret.xml */
if (rottenTomatoesApiKey.equals("REPLACE WITH YOUR KEY")) {
Toast.makeText(context, context.getString(R.string.error_replace_api_key), Toast.LENGTH_LONG).show();
}
}
/**
* 'IN THEATER' MOVIES
*/
@Subscribe
public void getMovies(final GetMoviesEvent event) {
/**
* rotten_tomatoes_api_key is stored in a strings resource file named secret.xml and
* located in res/values, but secret.xml has been added to .gitignore so my key doesn't
* end up in source control.
*
* simply create your own res/values/secret.xml file with your own Rotten Tomatoes API
* key and the app will run as expected.
*/
ApiService apiService = new RestClient(rottenTomatoesUrl, isRetrofitLoggingEnabled).getApiService();
apiService.getMovies(rottenTomatoesApiKey, event.getPageNumber(), event.getPageLimit(), new RestCallback(event.getCallNumber()) {
@Override
public void success(Movies movies, Response response) {
if (response.getBody().length() > 0) {
EventBus.post(movies);
} else {
EventBus.post(new APIErrorEvent(RetrofitError.unexpectedError(response.getUrl(), new HttpException("Empty Body")), event.getCallNumber()));
}
}
});
}
}
public abstract class RestCallback implements Callback {
private int callNumber;
public RestCallback(int callNumber) {
this.callNumber = callNumber;
}
@Override
public void failure(RetrofitError error) {
EventBus.post(new APIErrorEvent(error, this.callNumber));
}
public int getCallNumber() {
return callNumber;
}
}
public interface ApiService {
final static String APPSETTINGS = "/application_settings.json";
final static String MOVIES = "/in_theaters.json";
// IN THEATER MOVIES
@GET(MOVIES)
void getMovies(@Query("apikey") String key, @Query("page") int page, @Query("page_limit") int page_limit, Callback cb);
}