Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.media.Image;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.Uri;
- import android.nfc.Tag;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.appindexing.Action;
- import com.google.android.gms.appindexing.AppIndex;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationServices;
- import com.lakimens.lweather.R;
- import com.lakimens.lweather.weather.Current;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
- public static final String TAG = MainActivity.class.getSimpleName();
- @BindView(R.id.locateBTN)
- ImageView mLocateButton;
- @BindView(R.id.locationTV)
- TextView mLocationText;
- @BindView(R.id.tempTV)
- TextView mTempText;
- @BindView(R.id.timeTV)
- TextView mTimeText;
- @BindView(R.id.locationET)
- EditText mLocationEText;
- private Current mCurrent;
- private Location mLocation;
- private GoogleApiClient mClient;
- double latitude;
- double longtitude;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- String APIKey = "HIDDEN FOR OBVIOUS REASONS";
- mClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- Uri.Builder builder = new Uri.Builder();
- builder.scheme("https")
- .authority("api.forecast.io")
- .appendPath("forecast")
- .appendPath(APIKey)
- .appendPath(longtitude + "," + latitude)
- .appendQueryParameter("units", "si");
- String URL = builder.build().toString();
- if (isNetworkAvailible()) {
- OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder()
- .url(URL)
- .build();
- Call call = client.newCall(request);
- call.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();
- Log.v(TAG, jsonData);
- if (response.isSuccessful()) {
- try {
- mCurrent = getData(jsonData);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mTempText.setText(longtitude + "," + latitude);
- }
- });
- Log.i(TAG, "LNG: " + longtitude);
- Log.i(TAG, "LAT: " + latitude);
- } catch (JSONException e) {
- Log.e(TAG, "Exception caught:", e);
- }
- }
- }
- });
- } else {
- Toast.makeText(this, "Can't get DATA", Toast.LENGTH_SHORT).show();
- }
- }
- public boolean isNetworkAvailible() {
- ConnectivityManager manager = (ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = manager.getActiveNetworkInfo();
- boolean isAvailible = false;
- if (netInfo != null) {
- isAvailible = true;
- }
- return isAvailible;
- }
- private Current getData(String jsonData) throws JSONException {
- JSONObject forecast = new JSONObject(jsonData);
- String timezone = forecast.getString("timezone");
- Log.i(TAG, "JSON" + timezone);
- JSONObject currently = forecast.getJSONObject("currently");
- Current Current = new Current();
- Current.setTime(currently.getLong("time"));
- Current.setTemp(currently.getDouble("temperature"));
- Current.setIcon(currently.getString("icon"));
- Current.setPrecip(currently.getDouble("precipProbability"));
- Current.setSummary(currently.getString("summary"));
- Current.setTimeZone(timezone);
- Log.d(TAG, Current.getFormattedTime());
- return Current;
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED
- && ActivityCompat.checkSelfPermission
- (this, Manifest.permission.ACCESS_COARSE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- return;
- }
- mLocation = LocationServices.FusedLocationApi.getLastLocation(mClient);
- if(mLocation != null){
- latitude = mLocation.getLatitude();
- longtitude = mLocation.getLongitude();
- }
- else{
- Toast.makeText(this, "Location not detected", Toast.LENGTH_SHORT);
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- Log.i(TAG, "Connection Suspended");
- mClient.connect();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Log.i(TAG, "Connection Failed: " + connectionResult.getErrorCode());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement