1
votes

I have a string array and i want to call Retrofit so many times as size of this array actually is. But right now despite the fact that I've declared loop and call Retrofit inside of this loop, when I run in debug mode I see that my app never enter onResponse in ApiHit method unless he reach end of for loop. Then finnaly he do what i want to do in every loop iterate (enter to onResponse and execute rest of my code which is included). Now every iteration call:

call = tagApi.getTasks(tags);
call.enqueue(new Callback<Api>()

and goes to the end of method and skip onResponse and onFailure. Why?

I call for Retrofit in onCreate method:

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;
            }
        });

    }

and my ApiHit method:

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) {
                }

            });

        }

and Api class:

public class 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;
}

}

1

1 Answers

0
votes

if retrofit skips onResponse and onFailure methods, then you have a error in GET, POST method. check your method type properly. I have the problem once.