【问题标题】:Reterofit not returning Response Android改造不返回响应Android
【发布时间】:2016-11-01 15:38:37
【问题描述】:

我是改装新手,并试图通过Reterofit Tutorial 的教程赶上它,但我无法从中得到任何回应。我曾尝试在 onResponse 和 onFailure 内使用断点调试应用程序,但无法达到这些断点。并且不知道哪里出错了。 MainActivity:

public class MainActivity extends AppCompatActivity  {
    Toolbar toolbar;
    ProgressDialog progressDialog=null;
    public static final String Base_Url = "https://api.stackexchange.com";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.appBar);
        //set support for toolbar
        setSupportActionBar(toolbar);
        ReteroFiller serverGet = new ReteroFiller();
        serverGet.execute();

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onPrepareOptionsMenu(menu);
    }

    private class ReteroFiller extends AsyncTask<String ,String,String> implements Callback<StackOverFlowQuestions>{

        @Override
        protected String doInBackground(String... strings) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://api.stackexchange.com")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            // prepare call in Retrofit 2.0
            StackOverFlowAPI stackOverflowAPI = retrofit.create(StackOverFlowAPI.class);

            Call<StackOverFlowQuestions> call = stackOverflowAPI.loadQuestions("android");
            //asynchronous call
           // call.enqueue();

            // synchronous call would be with execute, in this case you
            // would have to perform this outside the main thread
            // call.execute()

            // to cancel a running request
            // call.cancel();
            // calls can only be used once but you can easily clone them
            //Call<StackOverflowQuestions> c = call.clone();
            call.enqueue(this);

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation

           if(progressDialog!=null){
               progressDialog.dismiss();

           }

        }
        @Override
        protected void onPreExecute() {
           if(progressDialog==null){
               progressDialog = ProgressDialog.show(MainActivity.this,
                       "ProgressDialog",
                       "Progressing");
           }

        }


        @Override
        protected void onProgressUpdate(String... text) {


        }


        @Override
        public void onResponse(Call<StackOverFlowQuestions> call, Response<StackOverFlowQuestions> response) {
            Log.i("RETERO TEST : ","Message: "+response.message()+" Body : "+response.body());

        }

        @Override
        public void onFailure(Call<StackOverFlowQuestions> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}

StackOverFlowQuestions 类

public class StackOverFlowQuestions {
    List<Question> items;
}

问题模型类:

public class Question {

        String title;
        String link;

        @Override
        public String toString() {
            return(title);
        }
    }

StackOverFlowAPI:接口

public interface StackOverFlowAPI {
    @GET("/2.2/questions?order=desc&sort=creation&site=stackoverflow")
    Call<StackOverFlowQuestions> loadQuestions(
            @Query("tagged") String tags
    );
}

【问题讨论】:

  • 你的界面在哪里?
  • 为我工作。我会删除吐司消息。 call.enqueue 本身就是一个异步调用。所以你可以删除 asynctask
  • 我见过一些提到 RestAdapter 的东西,但特别是代码,甚至在他们的官方网站上也没有关于特定适配器的参考。那么什么时候使用呢?
  • 这可能看起来很奇怪,但请尝试将此 .baseUrl("api.stackexchange.com") 更改为此 .baseUrl("api.stackexchange.com/") 和此 @GET("/2.2/questions?order= desc&sort=creation&site=stackoverflow") 到这个@GET("2.2/questions?order=desc&sort=creation&site=stackoverflow")

标签: android retrofit2


【解决方案1】:

我刚刚删除了asynctask,它工作正常,这是我的活动

public class MainActivity extends AppCompatActivity implements Callback<StackOverFlowQuestions> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.stackexchange.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        StackOverFlowAPI stackOverflowAPI = retrofit.create(StackOverFlowAPI.class);

        Call<StackOverFlowQuestions> call = stackOverflowAPI.loadQuestions("android");

        call.enqueue(this);

    }

    @Override
    public void onResponse(Call<StackOverFlowQuestions> call, Response<StackOverFlowQuestions> response) {
        Log.d("RETERO TEST : ","Message: "+response.message()+" Body : "+response.body());
    }

    @Override
    public void onFailure(Call<StackOverFlowQuestions> call, Throwable t) {
        Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
    }
}

我使用了你的其余代码,还添加了以下 gradle 依赖项

//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

【讨论】:

  • 我也试过了,谢谢。但无法获取 response.body().items 因为它无法识别项目
  • 这是因为您的模型没有根据您的 api 响应映射
【解决方案2】:

AFAIK,您忘记为您的retrofit 设置client:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.stackexchange.com")
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()) // this is what you need!
                .build();

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 2017-01-15
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多