Код: Выделить всё
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_weather_app, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
new GetWeatherAync().execute(getActivity());
return view;
}
private class GetWeatherAync extends AsyncTask {
private String TAG = GetWeatherAync.class.getSimpleName();
private Context mContext;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List doInBackground(Context...params) {
mContext = params[0];
return getWeatherFromServer();
}
@Override
protected void onPostExecute(List result) {
super.onPostExecute(result);
if (result != null) {
Log.e(TAG, "populate UI recycler view with gson converted data");
RecyclerViewAdapter weatherRecyclerViewAdapter = new RecyclerViewAdapter(result, mContext);
mRecyclerView.setAdapter(weatherRecyclerViewAdapter);
}
}
}
public List getWeatherFromServer(){
String serviceUrl = "http://api.openweathermap.org/data/2.5/forecast?q=" + searchView + api_key;
URL url = null;
try {
url = new URL(serviceUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(4000);
connection.setReadTimeout(4000);
connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//pass buffered reader to convert json to javaobject using gson
return convertJsonToObject(bufferedReader);
}catch (Exception e){
}
return null;
}
public List convertJsonToObject(BufferedReader bufferedReader){
final Gson gson = new Gson();
//pass root element type to fromJson method along with input stream
ForecastWeatherListWrapper weatherWrapper = gson.fromJson(bufferedReader,ForecastWeatherListWrapper.class);
List weatherlst = weatherWrapper.getforecastWeatherLists();
return weatherlst;
}
Код: Выделить всё
public class ForecastWeatherList {
@SerializedName("dt")
@Expose
private Integer dt;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("weather")
@Expose
private Weather weather = null;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("rain")
@Expose
private Rain rain;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("dt_txt")
@Expose
private String dtTxt;
public Integer getDt() {
return dt;
}
public void setDt(Integer dt) {
this.dt = dt;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public Weather getWeather() {
return (Weather) weather;
}
public void setWeather(Weather weather) {
this.weather = weather;
}
public Clouds getClouds() {
return clouds;
}
public void setClouds(Clouds clouds) {
this.clouds = clouds;
}
public Wind getWind() {
return wind;
}
public void setWind(Wind wind) {
this.wind = wind;
}
public Rain getRain() {
return rain;
}
public void setRain(Rain rain) {
this.rain = rain;
}
public Sys getSys() {
return sys;
}
public void setSys(Sys sys) {
this.sys = sys;
}
public String getDtTxt() {
return dtTxt;
}
public void setDtTxt(String dtTxt) {
this.dtTxt = dtTxt;
}
Вот оболочку, которую я использую.
Код: Выделить всё
public class ForecastWeatherListWrapper {
private List forecastWeatherLists;
public List getforecastWeatherLists() {
return forecastWeatherLists;
}
public void setforecastWeatherLists(List forecastWeatherItems){
this.forecastWeatherLists = forecastWeatherItems;
}
Я впервые использую GSON для анализа данных JSON, поэтому у меня вопрос: Я правильно использую его для анализа данных JSON, учитывая, что существуют другие классы, такие как Main, Clouds и т. д., и правильно ли я подключаюсь к Интернету с помощью API. Спасибо.
Вот мой основной класс:
public class Main {
@SerializedName("temp")
@Expose
private Double temp;
@SerializedName("temp_min")
@Expose
private Double tempMin;
@SerializedName("temp_max")
@Expose
private Double tempMax;
@SerializedName("pressure")
@Expose
private Double pressure;
@SerializedName("sea_level")
@Expose
private Double seaLevel;
@SerializedName("grnd_level")
@Expose
private Double grndLevel;
@SerializedName("humidity")
@Expose
private Integer humidity;
@SerializedName("temp_kf")
@Expose
private Integer tempKf;
public Double getTemp() {
return temp;
}
public void setTemp(Double temp) {
this.temp = temp;
}
public Double getTempMin() {
return tempMin;
}
public void setTempMin(Double tempMin) {
this.tempMin = tempMin;
}
public Double getTempMax() {
return tempMax;
}
public void setTempMax(Double tempMax) {
this.tempMax = tempMax;
}
public Double getPressure() {
return pressure;
}
public void setPressure(Double pressure) {
this.pressure = pressure;
}
public Double getSeaLevel() {
return seaLevel;
}
public void setSeaLevel(Double seaLevel) {
this.seaLevel = seaLevel;
}
public Double getGrndLevel() {
return grndLevel;
}
public void setGrndLevel(Double grndLevel) {
this.grndLevel = grndLevel;
}
public Integer getHumidity() {
return humidity;
}
public void setHumidity(Integer humidity) {
this.humidity = humidity;
}
public Integer getTempKf() {
return tempKf;
}
public void setTempKf(Integer tempKf) {
this.tempKf = tempKf;
}
}
< /code>
Вот мои необработанные строковые данные (я думаю). < / p > < b r / > < b r / > < c o d e > " c o d " : " 2 0 0 " , < b r / > " m e s s a g e " : 0 . 0 0 7 4 , < b r / > " c n t " : 3 9 , < b r / > " l i s t " : [ < b r / > { < b r / > " d t " : 1 5 3 4 2 1 5 6 0 0 , < b r / > " m a i n " : { < b r / > " t e m p " : 2 9 3 . 2 4 , < b r / > " t e m p _ m i n " : 2 9 2 . 3 4 6 , < b r / > " t e m p _ m a x " : 2 9 3 . 2 4 , < b r / > " p r e s s u r e " : 1 0 2 1 . 7 7 , < b r / > " s e a _ l e v e l " : 1 0 2 8 . 2 1 , < b r / > " g r n d _ l e v e l " : 1 0 2 1 . 7 7 , < b r / > " h u m i d i t y " : 1 0 0 , < b r / > " t e m p _ k f " : 0 . 8 9 < b r / > } , < b r / > " w e a t h e r " : [ < b r / > { < b r / > " i d " : 5 0 0 , < b r / > " m a i n " : " R a i n " , < b r / > " d e s c r i p t i o n " : " l i g h t r a i n " , < b r / > " i c o n " : " 1 0 n " < b r / > } < b r / > ] , < b r / > " c l o u d s " : { < b r / > " a l l " : 2 0 < b r / > } , < b r / > " w i n d " : { < b r / > " s p e e d " : 2 . 5 1 , < b r / > " d e g " : 2 7 5 . 0 0 1 < b r / > } , < b r / > " r a i n " : { < b r / > " 3 h " : 0 . 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 8 < b r / > } , < b r / > " s y s " : { < b r / > " p o d " : " n " < b r / > } , < b r / > " d t _ t x t " : " 2 0 1 8 - 0 8 - 1 4 0 3 : 0 0 : 0 0 " < b r / > } , < b r / > { < b r / > " d t " : 1 5 3 4 2 2 6 4 0 0 , < b r / > " m a i n " : { < b r / > " t e m p " : 2 9 2 . 3 , < b r / > " t e m p _ m i n " : 2 9 1 . 7 0 6 , < b r / > " t e m p _ m a x " : 2 9 2 . 3 , < b r / > " p r e s s u r e " : 1 0 2 0 . 9 9 , < b r / > " s e a _ l e v e l " : 1 0 2 7 . 4 2 , < b r / > " g r n d _ l e v e l " : 1 0 2 0 . 9 9 , < b r / > " h u m i d i t y " : 1 0 0 , < b r / > " t e m p _ k f " : 0 . 6 < b r / > } , < b r / > " w e a t h e r " : [ < b r / > { < b r / > " i d " : 8 0 0 , < b r / > " m a i n " : " C l e a r " , < b r / > " d e s c r i p t i o n " : " c l e a r s k y " , < b r / > " i c o n " : " 0 1 n " < b r / > } < b r / > ] , < b r / > " c l o u d s " : { < b r / > " a l l " : 0 < b r / > } , < b r / > " w i n d " : { < b r / > " s p e e d " : 2 . 5 2 , < b r / > " d e g " : 2 9 4 . 5 0 5 < b r / > } , < b r / > " r a i n " : { < b r / > < b r / > } , < b r / > " s y s " : { < b r / > " p o d " : " n " < b r / > } , < b r / > " d t _ t x t " : " 2 0 1 8 - 0 8 - 1 4 0 6 : 0 0 : 0 0 " < b r / > } , < b r / > { < b r / > " d t " : 1 5 3 4 2 3 7 2 0 0 , < b r / > " m a i n " : { < b r / > " t e m p " : 2 9 1 . 0 7 , < b r / > " t e m p _ m i n " : 2 9 0 . 7 7 , < b r / > " t e m p _ m a x " : 2 9 1 . 0 7 , < b r / > " p r e s s u r e " : 1 0 2 0 . 6 5 , < b r / > " s e a _ l e v e l " : 1 0 2 7 . 0 3 , < b r / > " g r n d _ l e v e l " : 1 0 2 0 . 6 5 , < b r / > " h u m i d i t y " : 1 0 0 , < b r / > " t e m p _ k f " : 0 . 3 < b r / > } , < b r / > " w e a t h e r " : [ < b r / > { < b r / > " i d " : 8 0 0 , < b r / > " m a i n " : " C l e a r " , < b r / > " d e s c r i p t i o n " : " c l e a r s k y " , < b r / > " i c o n " : " 0 1 n " < b r / > } < b r / > ] , < b r / > " c l o u d s " : { < b r / > " a l l " : 0 < b r / > } , < b r / > " w i n d " : { < b r / > " s p e e d " : 1 . 3 1 , < b r / > " d e g " : 2 2 5 . 5 < b r / > } , < b r / > " r a i n " : { < b r / > < b r / > } , < b r / > " s y s " : { < b r / > " p o d " : " n " < b r / > } , < b r / > " d t _ t x t " : " 2 0 1 8 - 0 8 - 1 4 0 9 : 0 0 : 0 0 " < b r / > } , < b r / > { < b r / > " d t " : 1 5 3 4 2 4 8 0 0 0 , < b r / > " m a i n " : { < b r / > " t e m p " : 2 9 3 . 2 8 6 , < b r / > " t e m p _ m i n " : 2 9 3 . 2 8 6 , < b r / > " t e m p _ m a x " : 2 9 3 . 2 8 6 , < b r / > " p r e s s u r e " : 1 0 2 0 . 7 8 , < b r / > " s e a _ l e v e l " : 1 0 2 7 . 1 7 , < b r / > " g r n d _ l e v e l " : 1 0 2 0 . 7 8 , < b r / > " h u m i d i t y " : 1 0 0 , < b r / > " t e m p _ k f " : 0 < b r / > } , < b r / > " w e a t h e r " : [ < b r / > { < b r / > " i d " : 8 0 0 , < b r / > " m a i n " : " C l e a r " , < b r / > " d e s c r i p t i o n " : " c l e a r s k y " , < b r / > " i c o n " : " 0 2 d " < b r / > } < b r / > ] , < b r / > " c l o u d s " : { < b r / > " a l l " : 8 < b r / > } , < b r / > " w i n d " : { < b r / > " s p e e d " : 2 . 8 3 , < b r / > " d e g " : 2 3 4 . 5 0 1 < b r / > } , < b r / > " r a i n " : { < b r / > < b r / > } , < b r / > " s y s " : { < b r / > " p o d " : " d " < b r / > } , < b r / > " d t _ t x t " : " 2 0 1 8 - 0 8 - 1 4 1 2 : 0 0 : 0 0 " < b r / > } , < b r / > { < b r / > " d t " : 1 5 3 4 2 5 8 8 0 0 , < b r / > " m a i n " : { < b r / > " t e m p " : 2 9 8 . 6 7 1 , < b r / > " t e m p _ m i n " : 2 9 8 . 6 7 1 , < b r / > " t e m p _ m a x " : 2 9 8 . 6 7 1 , < b r / > " p r e s s u r e " : 1 0 2 0 . 7 6 , < b r / > " s e a _ l e v e l " : 1 0 2 7 . 1 5 , < b r / > " g r n d _ l e v e l " : 1 0 2 0 . 7 6 , < b r / > " h u m i d i t y " : 9 2 , < b r / > " t e m p _ k f " : 0 < b r / > } , < b r / > " w e a t h e r " : [ < b r / > { < b r / > " i d " : 8 0 0 , < b r / > " m a i n " : " C l e a r " , < b r / > " d e s c r i p t i o n " : " c l e a r s k y " , < b r / > " i c o n " : " 0 1 d " < b r / > } < b r / > ] , < b r / > " c l o u d s " : { < b r / > " a l l " : 0 < b r / > } , < b r / > " w i n d " : { < b r / > " s p e e d " : 2 . 7 1 , < b r / > " d e g " : 2 5 9 . 5 < b r / > } , < b r / > " r a i n " : { < b r / > < b r / > } , < b r / > " s y s " : { < b r / > " p o d " : " d " < b r / > } , < b r / > " d t _ t x t " : " 2 0 1 8 - 0 8 - 1 4 1 5 : 0 0 : 0 0 " < b r / > } , < b r / > { < b r / > " d t " : 1 5 3 4 2 6 9 6 0 0 , < b r / > " m a i n " : { < b r / > "temp":300.7,
"temp_min":300.7,
"temp_max":300.7,
"pressure":1019.76,
"sea_level":1026.18,
"grnd_level":1019.76,
"humidity":83,
"temp_kf":0
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10d"
}
],
"clouds":{
"all":24
},
"wind":{
"speed":3.66,
"deg":285.503
},
"rain":{
"3h":1.11
},
"sys":{
"pod":"d"
},
"dt_txt":"2018-08-14 18:00:00"
},
{
"dt":1534280400,
"main":{
"temp":298.464,
"temp_min":298.464,
"temp_max":298.464,
"pressure":1019.68,
"sea_level":1025.97,
"grnd_level":1019.68,
"humidity":83,
"temp_kf":0
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10d"
}
],
"clouds":{
"all":48
},
"wind":{
"speed":3.27,
"deg":289.504
},
"rain":{
"3h":1.61
},
"sys":{
"pod":"d"
},
"dt_txt":"2018-08-14 21:00:00"
},
{
"dt":1534291200,
"main":{
"temp":297.882,
"temp_min":297.882,
"temp_max":297.882,
"pressure":1020,
"sea_level":1026.37,
"grnd_level":1020,
"humidity":82,
"temp_kf":0
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10n"
}
],
"clouds":{
"all":36
},
"wind":{
"speed":2.37,
"deg":275.004
},
"rain":{
"3h":0.13
},
"sys":{
"pod":"n"
},
"dt_txt":"2018-08-15 00:00:00"
},
{
"dt":1534302000,
"main":{
"temp":295.242,
"temp_min":295.242,
"temp_max":295.242,
"pressure":1021.11,
"sea_level":1027.53,
"grnd_level":1021.11,
"humidity":94,
"temp_kf":0
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03n"
}
],
"clouds":{
"all":32
},
"wind":{
"speed":1.26,
"deg":313.002
},
"rain":{
},
"sys":{
"pod":"n"
},
"dt_txt":"2018-08-15 03:00:00"
},
{
"dt":1534312800,
"main":{
"temp":294.05,
"temp_min":294.05,
"temp_max":294.05,
"pressure":1021.27,
"sea_level":1027.77,
"grnd_level":1021.27,
"humidity":100,
"temp_kf":0
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01n"
}
],
"clouds":{
"all":0
},
"wind":{
"speed":2.46,
"deg":274.504
},
"rain":{
},
"sys":{
"pod":"n"
},
"dt_txt":"2018-08-15 06:00:00"
},
{
"dt":1534323600,
"main":{
"temp":293.495,
"temp_min":293.495,
"temp_max":293.495,
"pressure":1021.36,
"sea_level":1027.7,
"grnd_level":1021.36,
"humidity":100,
"temp_kf":0
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01n"
}
],
"clouds":{
"all":0
},
"wind":{
"speed":3.01,
"deg":277.505
},
"rain":{
},
"sys":{
"pod":"n"
},
"dt_txt":"2018-08-15 09:00:00"
],
"city":{
"id":4347778,
"name":"Baltimore",
"coord":{
"lat":39.2909,
"lon":-76.6108
},
"country":"US",
"population":620961
< /code>
}
} < /p>
Подробнее здесь: https://stackoverflow.com/questions/518 ... -correctly