我有一个字符串数组,我想调用Retrofit这么多次,因为这个数组的大小实际上是。但是现在尽管我已经声明循环并在此循环中调用Retrofit,但当我在调试模式下运行时,我看到我的应用程序永远不会在ApiHit方法中输入onResponse,除非他到达for循环结束。然后finnaly他做我想在每个循环迭代中做的事情(输入onResponse并执行包含的其余代码)。现在每次迭代调用:
call = tagApi.getTasks(tags);
call.enqueue(new Callback<Api>()
并转到方法的结尾并跳过onResponse和onFailure。为什么呢?
我在onCreate方法中调用Retrofit:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//hide toolbar
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
ButterKnife.bind(this);
listTags = new ArrayList<>();
listTagsFinal = new ArrayList<>();
listTagsRest = new ArrayList<>();
strings = new ArrayList<>();
//search text handler
svSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
result = query.split(",?\\ ");
//numberTag = result.length;
for (int i = 0; i < result.length; i++) {
ApiHit(result[i]);
}
FinalSort();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
//This is your adapter that will be filtered
return false;
}
});
}
和我的ApiHit方法:
public void ApiHit(final String tags) {
//retrofit
TagApi tagApi = NetworkService.retrofit.create(TagApi.class);
Call<Api> call;
//recieve words from searchview, divide and put into string array
call = tagApi.getTasks(tags);
call.enqueue(new Callback<Api>() {
@Override
public void onResponse(Call<Api> call, Response<Api> response) {
//recieve possible tags for one word and put it into list
for (int i = 0; i < response.body().getResults().size(); i++) {
listTags.add(new RowModel(response.body().getResults().get(i).getTag(), response.body().getResults().get(i).getAbsRelevance()));
}
//sort whole list
Collections.sort(listTags);
//add first tags to list
if (listTags.size()>0) {
for (int y = 0; y < 3 && y < listTags.size() ; y++) {
listTagsFinal.add(new RowModel(listTags.get(y).getName(), listTags.get(y).getPosition()));
}
}
if (!isTagognizerTag) {
listTagsFinal.add(new RowModel("tagognizer", 0.0022055893));
isTagognizerTag = true;
}
//add rest tags to list
for (int y = 3; y < listTags.size(); y++) {
listTagsRest.add(new RowModel(listTags.get(y).getName(), listTags.get(y).getPosition()));
}
//create final array when response reach last tag
/* if (checker == tags.length) {
//final sort
FinalSort();
}*/
listTags.clear();
checker++;
}
@Override
public void onFailure(Call<Api> call, Throwable t) {
}
});
}
和Api类:
公共类Api {
@SerializedName("geo")
@Expose
private List<Float> geo = null;
@SerializedName("rank")
@Expose
private int rank;
@SerializedName("results")
@Expose
private List<Result> results = null;
@SerializedName("tag")
@Expose
private String tag;
@SerializedName("tagExists")
@Expose
private boolean tagExists;
public List<Float> getGeo() {
return geo;
}
public void setGeo(List<Float> geo) {
this.geo = geo;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public boolean isTagExists() {
return tagExists;
}
public void setTagExists(boolean tagExists) {
this.tagExists = tagExists;
}
}
答案 0 :(得分:0)
如果改装跳过onResponse和onFailure方法,则GET,POST方法出错。正确检查您的方法类型。 我有一次这个问题。