Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomeScreen extends Fragment{
- int currentWeather = 0;
- onViewCreated(){
- curWeather = ... initialisation.
- curWeather.setText(""+getCurrentWeather);
- }
- public int getCurrentWeather(){
- OkHttpClient okHttpClient = new OkHttpClient();
- Request re = new Request.Builder().url(forecastURL).build();
- Call call = okHttpClient.newCall(re);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- String jsonResponse = response.body().string();
- JSONObject forecast;
- JSONObject currently;
- try {
- forecast = new JSONObject(jsonResponse);
- currently = forecast.getJSONObject("currently");
- currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
- currentTempCelc = (currentTempFaron - 32) * 5 / 9;
- Log.d("weather in try", "" + currentTempCelc); // has weather value
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Log.d("weather in response", "" + currentTempCelc); // again has weather value.
- }
- });
- return currentTempCelc; //nothing it has at this point after coming out of response.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement