Advertisement
Guest User

Untitled

a guest
May 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. public class HomeScreen extends Fragment{
  2. int currentWeather = 0;
  3.  
  4. onViewCreated(){
  5. curWeather = ... initialisation.
  6. curWeather.setText(""+getCurrentWeather);
  7. }
  8.  
  9. public int getCurrentWeather(){
  10.  
  11. OkHttpClient okHttpClient = new OkHttpClient();
  12. Request re = new Request.Builder().url(forecastURL).build();
  13. Call call = okHttpClient.newCall(re);
  14. call.enqueue(new Callback() {
  15. @Override
  16. public void onFailure(Call call, IOException e) {
  17.  
  18. }
  19.  
  20. @Override
  21. public void onResponse(Call call, Response response) throws IOException {
  22. String jsonResponse = response.body().string();
  23. JSONObject forecast;
  24. JSONObject currently;
  25. try {
  26. forecast = new JSONObject(jsonResponse);
  27. currently = forecast.getJSONObject("currently");
  28. currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
  29. currentTempCelc = (currentTempFaron - 32) * 5 / 9;
  30.  
  31. Log.d("weather in try", "" + currentTempCelc); // has weather value
  32. } catch (JSONException e) {
  33. e.printStackTrace();
  34. }
  35. Log.d("weather in response", "" + currentTempCelc); // again has weather value.
  36. }
  37. });
  38. return currentTempCelc; //nothing it has at this point after coming out of response.
  39. }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement