【发布时间】: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")