Search in sources :

Example 76 with Callback

use of retrofit2.Callback in project gce2retrofit by chiuki.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final TextView textView = new TextView(this);
    setContentView(textView);
    Retrofit restAdapter = new Retrofit.Builder().baseUrl(API_URL).addConverterFactory(GsonConverterFactory.create()).build();
    Apis apis = restAdapter.create(Apis.class);
    Call<DirectoryList> call = apis.list(null, null);
    call.enqueue(new Callback<DirectoryList>() {

        @Override
        public void onResponse(Call<DirectoryList> call, Response<DirectoryList> response) {
            textView.setText(response.body().kind);
        }

        @Override
        public void onFailure(Call<DirectoryList> call, Throwable t) {
            String msg = t.getMessage();
            if (TextUtils.isEmpty(msg)) {
                textView.setText(t.getClass().getSimpleName());
            }
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) Apis(com.googleapis.www.Apis) TextView(android.widget.TextView) DirectoryList(com.googleapis.www.model.DirectoryList)

Example 77 with Callback

use of retrofit2.Callback in project BBS-Android by bdpqchen.

the class CollectionClient method deleteCollection.

void deleteCollection(int tid) {
    Retrofit retrofit = new Retrofit.Builder().client(client).addConverterFactory(GsonConverterFactory.create()).baseUrl("https://bbs.twtstudio.com/api/home/").build();
    CollectionApi collectionApi = retrofit.create(CollectionApi.class);
    Call<SimpleBean> call = collectionApi.deleteCollection(uidToken, String.valueOf(tid));
    call.enqueue(new Callback<SimpleBean>() {

        @Override
        public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
            collectionPresenter.dealDeleteData(response.body());
        }

        @Override
        public void onFailure(Call<SimpleBean> call, Throwable t) {
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) SimpleBean(com.twtstudio.bbs.bdpqchen.bbs.individual.model.SimpleBean)

Example 78 with Callback

use of retrofit2.Callback in project MVCHelper by LuckyJayce.

the class BooksOkHttpNormal_DataSource method loadBooks.

private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
    // 这里只是简单的演示OKhttp,你可以再封装哦
    FormBody.Builder formEncodingBuilder = new FormBody.Builder();
    formEncodingBuilder.add("api_key", "75ee6c644cad38dc8e53d3598c8e6b6c");
    Request request = new Request.Builder().url("https://www.baidu.com").post(formEncodingBuilder.build()).build();
    Call call = OkHttpUtils.client.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            // send 要放在最后一句(请求结束)
            sender.sendError(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final List<Book> books = new ArrayList<Book>();
            for (int i = 0; i < 15; i++) {
                books.add(new Book("page" + page + "  Java编程思想 " + i, 108.00d));
            }
            mPage = page;
            // send 要放在最后一句(请求结束)
            sender.sendData(books);
        }
    });
    return new OKHttpRequestHandle(call);
}
Also used : Call(okhttp3.Call) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) Book(com.shizhefei.test.models.enties.Book) ArrayList(java.util.ArrayList) List(java.util.List)

Example 79 with Callback

use of retrofit2.Callback in project iNGAGE by davis123123.

the class UserRecentCommentHandler method enqueue.

// get all recent comments for each room, for the user
public void enqueue(String username) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);
    Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
    Interface service = retrofit.create(Interface.class);
    Call<ResponseBody> call = service.get(username);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.i("STATE", "Retrofit response code: " + response.code());
            if (response.isSuccessful()) {
                Log.i("STATE", "Retrofit POST Success");
                try {
                    serverResponse = response.body().string();
                    createCommentsList(serverResponse);
                    callBackData.notifyChange();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("Retrofit Error Code:", String.valueOf(response.code()));
                Log.i("Retrofit Error Body", response.errorBody().toString());
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.i("STATE", "Retrofit Failure");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 80 with Callback

use of retrofit2.Callback in project WeexErosFramework by bmfe.

the class DefaultWXHttpAdapter method fetchUrl.

private void fetchUrl(final WXRequest request, final OnHttpListener listener) {
    final WXResponse wxResponse = new WXResponse();
    String method = request.method == null ? "GET" : request.method.toUpperCase();
    String requestBodyString = request.body == null ? "{}" : request.body;
    RequestBody body = null;
    if (request.paramMap != null && request.paramMap.containsKey("Content-Type")) {
        body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse(request.paramMap.get("Content-Type")), requestBodyString) : null;
    } else {
        body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), requestBodyString) : null;
    }
    Request.Builder requestBuilder = new Request.Builder().url(request.url).method(method, body);
    if (request.paramMap != null) {
        for (Map.Entry<String, String> param : request.paramMap.entrySet()) {
            requestBuilder.addHeader(param.getKey(), TextUtil.toHumanReadableAscii(param.getValue()));
        }
    }
    client.newCall(requestBuilder.build()).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            wxResponse.errorMsg = e.getMessage();
            wxResponse.errorCode = "-1";
            wxResponse.statusCode = "-1";
            if (listener != null) {
                listener.onHttpFinish(wxResponse);
            }
        }

        @Override
        public void onResponse(Call call, Response response) {
            byte[] responseBody = new byte[0];
            try {
                responseBody = response.body().bytes();
            } catch (IOException e) {
                e.printStackTrace();
                wxResponse.errorMsg = e.getMessage();
                wxResponse.errorCode = "-1";
                wxResponse.statusCode = "-1";
                if (listener != null) {
                    listener.onHttpFinish(wxResponse);
                }
            }
            wxResponse.data = new String(responseBody);
            wxResponse.statusCode = String.valueOf(response.code());
            wxResponse.originalData = responseBody;
            wxResponse.extendParams = new HashMap<>();
            for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
                wxResponse.extendParams.put(entry.getKey(), entry.getValue());
            }
            if (response.code() < 200 || response.code() > 299) {
                wxResponse.errorMsg = response.message();
                if (listener != null) {
                    listener.onHttpFinish(wxResponse);
                }
            } else {
                if (isInterceptor(request.url)) {
                    appendBaseJs(wxResponse, listener);
                    // listener);
                    if (listener != null) {
                        listener.onHttpFinish(wxResponse);
                    }
                } else {
                    // iconFont
                    if (listener != null) {
                        listener.onHttpFinish(wxResponse);
                    }
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) HashMap(java.util.HashMap) Request(okhttp3.Request) WXRequest(com.taobao.weex.common.WXRequest) IOException(java.io.IOException) WXResponse(com.taobao.weex.common.WXResponse) WXResponse(com.taobao.weex.common.WXResponse) Response(okhttp3.Response) Callback(okhttp3.Callback) Map(java.util.Map) HashMap(java.util.HashMap) RequestBody(okhttp3.RequestBody)

Aggregations

IOException (java.io.IOException)51 Response (okhttp3.Response)46 Call (okhttp3.Call)44 Callback (okhttp3.Callback)44 Request (okhttp3.Request)41 RequestBody (okhttp3.RequestBody)25 Call (retrofit2.Call)17 Callback (retrofit2.Callback)15 Test (org.junit.Test)14 Response (retrofit2.Response)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 FormBody (okhttp3.FormBody)12 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)12 OkHttpClient (okhttp3.OkHttpClient)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ResponseBody (okhttp3.ResponseBody)10 MockResponse (okhttp3.mockwebserver.MockResponse)10 Retrofit (retrofit2.Retrofit)9 TextView (android.widget.TextView)6 HttpUrl (okhttp3.HttpUrl)6