use of retrofit2.Callback in project coolweather by yeliheng.
the class AutoUpdateService method updateWeather.
/*
* 更新天气信息
* */
private void updateWeather() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String weatherString = prefs.getString("weather", null);
if (weatherString != null) {
Weather weather = Utility.handleWeatherResponse(weatherString);
String weatherId = weather.basic.weatherId;
// 请求地址
String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId + "&key=" + key;
HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseText = response.body().string();
Weather weather = Utility.handleWeatherResponse(responseText);
if (weather != null && "ok".equals(weather.status)) {
// 判断信息是否有效
// 存储数据到SD卡
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
editor.putString("weather", responseText);
editor.apply();
}
}
});
}
}
use of retrofit2.Callback in project My-MVP by REBOOTERS.
the class Retrofit2DemoActivity method SimpleRetrofit.
private void SimpleRetrofit() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL);
Retrofit retrofit = builder.client(httpClient.build()).build();
ApiService simpleService = retrofit.create(ApiService.class);
Call<ResponseBody> call = simpleService.getUserString(name);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
loading.dismiss();
try {
String result = response.body().string();
Gson gson = new Gson();
GithubUserBean bean = gson.fromJson(result, GithubUserBean.class);
setUserView(bean);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
loading.dismiss();
}
});
}
use of retrofit2.Callback in project okHttp-Util-gson by xwdz.
the class HttpManager method execute.
public void execute(final Request request, final CallBack callBack, final Class c) {
Call call = mClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (callBack != null) {
callBack.error(call, e);
}
}
@Override
public void onResponse(final Call call, final Response response) {
if (callBack != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (!response.isSuccessful()) {
IOException ioException = new IOException("request failed , reponse's code is : " + response.code());
callBack.error(call, ioException);
return;
}
if (call.isCanceled()) {
callBack.error(call, new IOException("Canceled!"));
return;
}
try {
final String json = response.body().string();
final Object object = Parser.getInstance().parser(json, c);
callBack.onNativeResponse(call, response);
callBack.response(call, object);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
});
mCalls.add(call);
}
use of retrofit2.Callback in project mapbox-events-android by mapbox.
the class TelemetryClientMapEventsTest method sendsTheCorrectBodyPostingMultipleEvents.
@Test
public void sendsTheCorrectBodyPostingMultipleEvents() throws Exception {
TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "anyUserAgent");
Event.Type load = Event.Type.MAP_LOAD;
Event loadEvent = obtainMapEvent(load);
Event.Type click = Event.Type.MAP_CLICK;
Event clickEvent = obtainMapEvent(click);
Callback mockedCallback = mock(Callback.class);
enqueueMockResponse();
List<Event> events = obtainEvents(loadEvent, clickEvent);
telemetryClient.sendEvents(events, mockedCallback);
String expectedRequestBody = obtainExpectedRequestBody(new GsonBuilder(), events.get(0), events.get(1));
assertRequestBodyEquals(expectedRequestBody);
}
use of retrofit2.Callback in project mapbox-events-android by mapbox.
the class TelemetryClientMapEventsTest method sendsTheCorrectBodyPostingMapLoadEvent.
@Test
public void sendsTheCorrectBodyPostingMapLoadEvent() throws Exception {
TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "anyUserAgent");
Event.Type load = Event.Type.MAP_LOAD;
Event aLoadEvent = obtainMapEvent(load);
List<Event> theLoadEvent = obtainEvents(aLoadEvent);
Callback mockedCallback = mock(Callback.class);
enqueueMockResponse();
telemetryClient.sendEvents(theLoadEvent, mockedCallback);
String expectedRequestBody = obtainExpectedRequestBody(new GsonBuilder(), theLoadEvent.get(0));
assertRequestBodyEquals(expectedRequestBody);
}
Aggregations