Good day.
Through okhttp3 I make an asynchronous request:
client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String jsonData = response.body().string(); try { JSONObject jobject = new JSONObject(jsonData); String id = jobject.getString("id"); //increment current id +1 String last_id = String.valueOf(Integer.parseInt(id)+1); Log.i("new id", last_id); if(response!=null){ response.getJsonResponse(last_id) } } catch (Exception e) { e.printStackTrace(); } //Log.i("ok", response.body().string()); } }); Interface for callback:
public interface getResponse { void getJsonResponse(final String id); } And actually launching a request from the Activity:
public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Helper helper = new Helper(); helper.getLastId(new getResponse(){ @Override void getJsonResponse(String id){ } }); }} } I can't understand how to get an Activity object from getJsonResponse from which I made a request (for example, you need to change an arbitrary TextView).
I tried already throwing this in the helper.getLastId () call and on all callbacks, but it still does not work, although in the debugger you can see that the correct Activity comes in getJsonResponse ().