user2659694 user2659694 - 5 months ago 30
JSON Question

Using retrofit to read string content from api

Currently, I am using Retrofit to get data from api. But the format of data is a bit different from other format such as :

["tayl",["taylor swift","taylor swift kanye west","taylor swift famous","taylor swift mp3","taylor lautner","taylor swift wiki","taylor swift 1989","taylor hill","taylor swift 2016","taylor kinney"]]


So, I want to ask for the best solution to parse values to get a list as below if I want to use retrofit:

"taylor swift","taylor swift kanye west","taylor swift famous","taylor swift mp3","taylor lautner","taylor swift wiki","taylor swift 1989","taylor hill","taylor swift 2016","taylor kinney"


The content of the file above is the data which GoogleAutoComplete Api returned for me with the link below :

http://suggestqueries.google.com/complete/search?client=firefox&q=tayl


I implemented the code as below but it is not good:

@Headers({
"Accept: application/json",
"Content-Type: application/json; charset=UTF-8"
})
@GET("complete/search?")
Call<ResponseBody> getAutoComplete(@Query(@Query("q")String query);


The below is response code which I am using:

autoCompleteCall = googleApi.getAutoComplete(client, keyword);
autoCompleteCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

if (response != null &&
response.body() != null) {
System.out.println(" String response======= " + response.body().toString());
return;
}

}

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

}

});


But the responsebody returned for me is null.
Please help me in this case.
Thanks.

Answer

Define the API endpoint in an interface as follows:

@GET("complete/search")
Call<ResponseBody> getAutoComplete(
        @Query("client") String client,
        @Query("q") String query);

Make the network request as follows:

Call<ResponseBody> call = service.getAutoComplete("firefox", "tayl");

call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response.isSuccessful()) {
            ResponseBody body = response.body();
            try {
                // autocompleteOptions => ["tayl",["taylor swift","taylor lautner",...
                String autocompleteOptions = body.string();
                JSONArray jsonArray = new JSONArray(autocompleteOptions).getJSONArray(1);
                // list => "taylor swift","taylor lautner",...
                ArrayList<String> list = GetAutocompleteOptions(jsonArray);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
         }
     }

     @Override
     public void onFailure(Call<ResponseBody> call, Throwable t) {
     }
});

private ArrayList<String> GetAutocompleteOptions(JSONArray jsonArray) throws JSONException {
    ArrayList<String> list = new ArrayList<>();
    if (jsonArray != null) {
        for (int i = 0; i < jsonArray.length(); i++) {
            list.add(jsonArray.get(i).toString());
        }
    }
    return list;
}