android xml tag has empty body,android – 将Retrofit RestAdapter.LogLevel设置为FULL以外的任何内容导致Empty Respons...

我的项目中有以下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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值