改造:如何等待响应 [英] Retrofit: How to wait for response

查看:131
本文介绍了改造:如何等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AsyncTask和doInBackground方法,在其中,我使用Retrofit发送POST请求.我的代码如下:

I have AsyncTask and the doInBackground method inside which, I sending POST request using Retrofit. My code looks like:

    //method of AsyncTask
    protected Boolean doInBackground(Void... params) {
        Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(Constants.ROOT_API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
        //request
        Call<JsonElement> result = service.getToken("TestUser", "pass", "password");
        result.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable t) {

            }
        });

        return true;
    }

问题是:改进了异步发送请求的过程,而doInBackground方法则返回该值.因此,我需要在序列中所有执行的同一线程中发送请求.逐个.从doInBackground返回是在请求完成之后发生的.如何使用Retrofit在同一线程中发送请求?

The problem is: Retrofit sending request asynchronously and while it, the doInBackground method returning the value. So I need to send a request in the same thread with all executions in the sequence. One by one. And returning from doInBackground occurs after the request finished. How can I send a request in the same thread using Retrofit?

推荐答案

Call 类具有

The Call class has an execute() method that will make your call synchronously.

enqueue()明确用于进行异步调用.

enqueue() is explicitly for making an asychronous call.

这篇关于改造:如何等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆