현재 기상 관측앱을 만드는 중입니다.
그래서 openweatherapi에 있는 값을 불러와서 메인 액티비티에 띄우는 작업중인데
값이 아무값도 뜨지 않네요.. url문제인것 같지는 않고 ...
사이트는 http://blog.naver.com/PostView.nhn?blogId=ivory82&logNo=220797022612&parentCategoryNo=&categoryNo=&v 참조했습니다..
제생각에는 AsyncTask쪽에 문제가 생겨서 불러오는게 안되는거 같은데 아무리 만져봐도 잘 모르겠네요..
고수님들 답변 제발 부탁드립니다 ㅠㅠ..
LocationManager locationManager;
private Context mContext;
boolean isGPSEnabled = false;
//현재 gps 사용유무
boolean isNetWorkEnabled = false;
//네트워크 사용 유무
boolean isGetLocation = false;
//gps 상태값
Location location;
double lat;
double lon;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
public Location getLocation() {
try {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//gps 정보 가져오기
isNetWorkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//현재 네트워크 상태 값 알아오기
if (!isGPSEnabled && !isNetWorkEnabled) {
} else {
this.isGetLocation = true;
try {
if (isNetWorkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
//네트워크 정보로부터 위치값 가져오기
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
//위도 경도 저장
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
}
} catch (SecurityException e) {
Log.d("error","오류",e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
getWeatherData(location.getLatitude(),location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
private void getWeatherData (double lat,double lon){
String url = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat +
"&lon=" + lon +"&units=metric&appid=081bed6a85d281e8a3b8f5d2903293f9";
ReceiveWeatherTask receiveWeatherTask = new ReceiveWeatherTask();
receiveWeatherTask.execute(url);
}
private class ReceiveWeatherTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... params) {
try{
HttpURLConnection conn = (HttpURLConnection) new URL(params[0]).openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader in = new BufferedReader(reader);
String readed;
while ((readed = in.readLine()) != null) {
JSONObject jsonObject = new JSONObject(readed);
return jsonObject;
}
}else {
return null;
}
return null;
} catch (Exception e) {
Log.d("Error","Cannot process JSON results",e);
}
return null;
}
@Override
protected void onPostExecute(JSONObject result) {
Log.i("error",result.toString());
if(result != null) {
String iconName="";
String nowTemp="";
String humidity="";
String main ="";
String description = "";
String city="";
String update="";
try{
iconName = result.getJSONArray("weather").getJSONObject(0).getString("icon");
nowTemp=result.getJSONObject("main").getString("temp");
humidity=result.getJSONObject("main").getString("humidity");
main=result.getJSONArray("weather").getJSONObject(0).getString("main");
description=result.getJSONArray("weather").getJSONObject(0).getString("description");
city=result.getString("name").toUpperCase(Locale.US) + "," +result.getJSONObject("sys")
.getString("country");
DateFormat df = DateFormat.getDateInstance();
update =df.format(new Date(result.getLong("dt")*1000));
icon.setText(iconName);
temp.setText(nowTemp);
Humidity.setText(humidity);
Cityfield.setText(city);
Update.setText(update);
} catch (JSONException e) {
Log.d("error","오류",e);
}
}
}
}
}