Advertisement
sshuvro58

Untitled

Apr 4th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. package teamtreehouse.com.stromy;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationManager;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.squareup.okhttp.Call;
  18. import com.squareup.okhttp.Callback;
  19. import com.squareup.okhttp.OkHttpClient;
  20. import com.squareup.okhttp.Request;
  21. import com.squareup.okhttp.Response;
  22.  
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.io.IOException;
  27. import java.util.List;
  28. import java.util.jar.JarException;
  29.  
  30. import butterknife.InjectView;
  31.  
  32.  
  33. public class MainActivity extends ActionBarActivity {
  34. public static final String TAG = MainActivity.class.getSimpleName();
  35. public double longitude;
  36. public double latitude;
  37. private CurrentWeather mCurrentWeather;
  38. @InjectView(R.id.timeLabel) TextView mTimeLabel;
  39. @InjectView(R.id.temperatureLabel) TextView mTemperatureLabel;
  40. @InjectView(R.id.humidityLabel) TextView mHumidityLabel;
  41. @InjectView(R.id.precipLabel) TextView mPrecipLabel;
  42. @InjectView(R.id.summaryLabel) TextView mSummaryLabel;
  43. @InjectView(R.id.iconImageView) ImageView mIconImageView;
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.activity_main);
  48. String apiKey = "e42a81c6e6fcdbb89f4e91e091b88682";
  49.  
  50.  
  51. String forecastURL ="https://api.forecast.io/forecast/" + apiKey + "/"+ longitude + ","+latitude;
  52. if(isNetworkAvailable()) {
  53. OkHttpClient client = new OkHttpClient();
  54. Request request = new Request.Builder()
  55. .url(forecastURL)
  56. .build();
  57.  
  58. Call call = client.newCall(request);
  59. call.enqueue(new Callback() {
  60. @Override
  61. public void onFailure(Request request, IOException e) {
  62.  
  63. }
  64.  
  65. @Override
  66. public void onResponse(Response response) throws IOException {
  67. try {
  68. String jsonData = response.body().string();
  69. Log.v(TAG, jsonData);
  70. if (response.isSuccessful()) {
  71. mCurrentWeather = getCurrentWeatherDetails(jsonData);
  72. } else {
  73. alertUserAboutError();
  74. }
  75. }
  76. catch (IOException e) {
  77. Log.e(TAG, "exception caught", e);
  78. }
  79. catch (JSONException e){
  80. Log.e(TAG, "exception caught", e);
  81. }
  82. }
  83. });
  84. }else {
  85. Toast.makeText(this,getString(R.string.network_unavailable_message),Toast.LENGTH_LONG).show();
  86. }
  87.  
  88. Log.d(TAG,"Main UI is running");
  89.  
  90.  
  91. }
  92. private CurrentWeather getCurrentWeatherDetails(String jsonData) throws JSONException{
  93. JSONObject forecast = new JSONObject(jsonData);
  94. String timezone = forecast.getString("timezone");
  95. Log.i(TAG,"from json"+timezone);
  96. JSONObject currently = forecast.getJSONObject("currently");
  97.  
  98. CurrentWeather currentWeather = new CurrentWeather();
  99. currentWeather.setHumidity(currently.getDouble("humidity"));
  100. currentWeather.setTime(currently.getLong("time"));
  101. currentWeather.setIcon(currently.getString("icon"));
  102. currentWeather.setPercipChance(currently.getDouble("precipProbability"));
  103. currentWeather.setSummary(currently.getString("summary"));
  104. currentWeather.setTemperature(currently.getDouble("temperature"));
  105. currentWeather.setTimeZone(timezone);
  106.  
  107. Log.d(TAG, currentWeather.getFormattedTime());
  108.  
  109. return currentWeather;
  110.  
  111. }
  112.  
  113. private boolean isNetworkAvailable() {
  114. ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  115. NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  116. boolean isAvailable = false;
  117. if (networkInfo != null && networkInfo.isConnected()){
  118. isAvailable = true;
  119. }
  120. return isAvailable;
  121. }
  122.  
  123. private void alertUserAboutError() {
  124. AlertDialogFragment dialog = new AlertDialogFragment();
  125. dialog.show(getFragmentManager(),"error_dialog");
  126. }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement