Instantly share code, notes, and snippets.

@EdgeJH EdgeJH/Weather.java
Last active Sep 7, 2018

Embed
What would you like to do?
package com.edge.weather;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;
public class MainActivity extends AppCompatActivity implements LocationListener, View.OnClickListener {
LocationManager locationManager;
double latitude;
double longitude;
TextView latText, lonText, weather;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
private void initView() {
//뷰세팅
latText = (TextView) findViewById(R.id.latitude);
lonText = (TextView) findViewById(R.id.longitude);
weather = (TextView) findViewById(R.id.weather);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onLocationChanged(Location location) {
/*현재 위치에서 위도경도 값을 받아온뒤 우리는 지속해서 위도 경도를 읽어올것이 아니니
날씨 api에 위도경도 값을 넘겨주고 위치 정보 모니터링을 제거한다.*/
latitude = location.getLatitude();
longitude = location.getLongitude();
//위도 경도 텍스트뷰에 보여주기
latText.setText(String.valueOf(latitude));
lonText.setText(String.valueOf(longitude));
//날씨 가져오기 통신
getWeather(latitude, longitude);
//위치정보 모니터링 제거
locationManager.removeUpdates(MainActivity.this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//버튼 클릭시 현재위치의 날씨를 가져온다
case R.id.button:
if (locationManager != null) {
requestLocation();
}
break;
}
}
private void requestLocation() {
//사용자로 부터 위치정보 권한체크
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 0);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 1, this);
}
}
private interface ApiService {
//베이스 Url
String BASEURL = "http://apis.skplanetx.com/";
String APPKEY = "APPKEY값";
//get 메소드를 통한 http rest api 통신
@GET("weather/current/hourly")
Call<JsonObject> getHourly(@Header("appkey") String appKey, @Query("version") int version,
@Query("lat") double lat, @Query("lon") double lon);
}
private void getWeather(double latitude, double longitude) {
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
.baseUrl(ApiService.BASEURL)
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<JsonObject> call = apiService.getHourly(ApiService.APPKEY, 1, latitude, longitude);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(@NonNull Call<JsonObject> call, @NonNull Response<JsonObject> response) {
if (response.isSuccessful()) {
//날씨데이터를 받아옴
JsonObject object = response.body();
if (object != null) {
//데이터가 null 이 아니라면 날씨 데이터를 텍스트뷰로 보여주기
weather.setText(object.toString());
}
}
}
@Override
public void onFailure(@NonNull Call<JsonObject> call, @NonNull Throwable t) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.edge.weather.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="위도"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="17dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="23151615"
android:id="@+id/latitude"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="17dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="경도"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="17dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="3213132"
android:id="@+id/longitude"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="17dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="날씨 받아오기"
android:id="@+id/button"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="결과"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="17dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:id="@+id/weather"
android:textSize="17dp"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment