tengo una funcion en Java que envia una peticion HTTP a un servidor y me gustaria almacenar una respuesta fuera del callback. El problema es que al hacerlo, me sale un json vacio. He estado mirando y he visto que puede ser porque los callbacks se hacen en otro hilo distinto, pero no lo tengo muy claro. ¿Sabría alguien aclararme las dudas? Gracias de antemano JSONObject jsonWeather = new JSONObject(); public JSONObject requestWeatherlocation(double latitude, double longitude, Activity activity) { EnvironmentVariables env = new EnvironmentVariables(); String url = env.getOpenWeatherMapUrlWeather() + "?appid=" + env.getOpenWeatherMapApi() + "&lat=" + latitude + "&lon=" + longitude; OkHttpClient client = new OkHttpClient.Builder().build(); Request request = new Request.Builder().url(url).build(); Handler mHandler = new Handler(Looper.getMainLooper()); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { if (response.isSuccessful()) { try { jsonWeather = new JSONObject(response.body().string()); } catch (JSONException e) { e.printStackTrace(); } } } }); return jsonWeather; } *****ACTUALIZACION***** He añadido el metodo: public void setJsonWeather(JSONObject jsonWeather){ this.jsonWeather = jsonWeather; } Y en el metodo `onResponse`, el try he añadido: try { jsonWeather = new JSONObject(response.body().string()); setJsonWeather(jsonWeather); } catch (JSONException e) { e.printStackTrace(); }