Have a look at below code:
public String getQuote() {
final String[] fullQuoteData = {""};
OkHttpClient client = new OkHttpClient();
okhttp3.Request request = new okhttp3.Request.Builder()
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
try{
res = response.body().string();
myResponse = new JSONArray(res);
if(myResponse != null && myResponse.length() > 0){
String quote = "", author = "", category = "";
for (int i = 0; i < myResponse.length(); i++) {
JSONObject object = myResponse.getJSONObject(i);
quote = object.getString("quote");
author = object.getString("author");
category = object.getString("category");
}
fullQuoteData[0] = quote + "-" + author + "-" + category;
}
} catch (JSONException e) {
e.printStackTrace();
}
//
}
return fullQuoteData[0];
THIS IS AN ASYNC like TASK and after completing network call I need to return a STRING then why I need to make it FINAL variable
And why it forces me to make the String as ARRAY 😭
Any hints!