Search in sources :

Example 96 with Callback

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();
                }
            }
        });
    }
}
Also used : Weather(com.coolweather.android.gson.Weather) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException)

Example 97 with Callback

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();
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GithubUserBean(home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean) Gson(com.google.gson.Gson) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit)

Example 98 with Callback

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);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) IOException(java.io.IOException)

Example 99 with Callback

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);
}
Also used : Callback(okhttp3.Callback) GsonBuilder(com.google.gson.GsonBuilder) Test(org.junit.Test)

Example 100 with Callback

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);
}
Also used : Callback(okhttp3.Callback) GsonBuilder(com.google.gson.GsonBuilder) Test(org.junit.Test)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)151 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Call (retrofit2.Call)90 Retrofit (retrofit2.Retrofit)85 ResponseBody (okhttp3.ResponseBody)64 Test (org.junit.Test)56 GsonBuilder (com.google.gson.GsonBuilder)52 Gson (com.google.gson.Gson)46 OkHttpClient (okhttp3.OkHttpClient)44 Response (retrofit2.Response)43 Callback (retrofit2.Callback)42 RequestBody (okhttp3.RequestBody)39 List (java.util.List)34 File (java.io.File)29 CountDownLatch (java.util.concurrent.CountDownLatch)26 Context (android.content.Context)24 RetrofitAPI (com.gladysinc.gladys.Utils.RetrofitAPI)21