Last active
July 8, 2016 03:36
-
-
Save huantt/8572660810b21b44a34a99cd9706b453 to your computer and use it in GitHub Desktop.
Xử lý json từ api openWeatherMap để lấy thông tin thời tiết
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
/** | |
* Created by Huan on 7/8/2016. | |
*/ | |
public class Coord { | |
float lon; | |
float lat; | |
public Coord(float lon, float lat) { | |
this.lon = lon; | |
this.lat = lat; | |
} | |
public float getLon() { | |
return lon; | |
} | |
public float getLat() { | |
return lat; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
import java.io.IOException; | |
/** | |
* Created by Huan on 7/7/2016. | |
*/ | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
WeatherForecast weatherForecast = new WeatherForecast("HaNoi","VietNam"); | |
Coord coord = weatherForecast.getCoord(); | |
//System.out.println(coord.getLon()); | |
System.out.println(weatherForecast.getWeather().toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
/** | |
* Created by Huan on 7/8/2016. | |
*/ | |
public class Main2 { | |
public static void main(String[] args) { | |
WeatherForecast w = new WeatherForecast("HaNoi", "VietNam"); | |
System.out.println(w.getWeatherData().getCoord().getLat()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
/** | |
* Created by Huan on 7/8/2016. | |
*/ | |
public class Weather { | |
private int id; | |
private String description; | |
private String main; | |
public Weather(int id, String description, String main) { | |
this.id = id; | |
this.description = description; | |
this.main = main; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public String getMain() { | |
return main; | |
} | |
@Override | |
public String toString() { | |
return "Weather: " + | |
"\nid = " + id + | |
"\ndescription = '" + description + '\'' + | |
"\nweather: "+ main; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
/** | |
* Created by Huan on 7/8/2016. | |
*/ | |
public class WeatherData { | |
private Coord coord; | |
private Weather[] weather; | |
public WeatherData(Coord coord, Weather[] weather) { | |
this.coord = coord; | |
this.weather = weather; | |
} | |
public Coord getCoord() { | |
return coord; | |
} | |
public Weather[] getWeather() { | |
return weather; | |
} | |
} | |
//Cứ thấy ngoặc [] thì nó là mảng, và coi data đó là 1 object thì đây là 1 mảng object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.huantt; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
/** | |
* Created by Huan on 7/8/2016. | |
*/ | |
public class WeatherForecast { | |
private String city; | |
private String contry; | |
private Gson gson; | |
private JsonParser jParser; | |
private JsonObject jsonObject; | |
public WeatherForecast(String city, String contry) { | |
this.city = city; | |
this.contry = contry; | |
gson = new Gson(); | |
// jParser = new JsonParser(); | |
// jsonObject = (JsonObject) jParser.parse(getJson(getLink())); | |
//Dùng jsonObject là 1 cách, nhưng có thể chỉ cần dùng Gson thôi | |
// Như method getWeatherData chỉ dùng gson nó sẽ tự tìm field trong Object WeatherData | |
// Thấy field nào tên giống thì nó lưu dữ liệu vào | |
} | |
// public Weather getWeather() { | |
// String weatherJson = jsonObject.get("weather").toString().replace("[", "").replace("]", ""); | |
// Weather weather = gson.fromJson(weatherJson, Weather.class); | |
// return weather; | |
// } | |
// | |
// public Coord getCoord() { | |
// Coord coord = gson.fromJson(jsonObject.get("coord"), Coord.class); | |
// return coord; | |
// } | |
public WeatherData getWeatherData() { | |
WeatherData weatherData = gson.fromJson(getJson(getLink()), WeatherData.class); | |
return weatherData; | |
} | |
String getJson(String link) { | |
StringBuilder data = new StringBuilder(); | |
try { | |
URL url = new URL(link); | |
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); | |
String line; | |
while ((line = in.readLine()) != null) | |
data.append(line); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return data.toString(); | |
} | |
String getLink() { | |
String link = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + contry + "&appid=cabc9614649278ff314eb4f62e95942e"; | |
return link; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment