'what is the wrong with the next method?
private void getWeatherInfo(String cityName){ String url = "http://api.weatherapi.com/v1/forecast.json?key=809499b55e964cebb8a10031220903&q="+cityName+"&days=1&aqi=yes&alerts=yes"; cityNameTV.setText(cityName); RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { loadingPB.setVisibility(View.GONE); homeRL.setVisibility(View.VISIBLE); weatherRVModelArrayList.clear(); try { String temperature = response.getJSONObject("current").getString("temp_c"); TemperatureTV.setText(temperature+"°c"); int isDay = response.getJSONObject("current").getInt("is_day");
String condition = response.getJSONObject("current").getJSONObject("condition").getString("text");
String conditionIcon = response.getJSONObject("current").getJSONObject("condition").getString("icon");
Picasso.get().load("http:".concat(conditionIcon)).into(iconIV);
ConditionTV.setText(condition);
if(isDay==1){
//morning
Picasso.get().load("https://img.sheroes.in/img/uploads/post/1190444_201812170411327145.cropped7146093298356274938.jpg?tr=w-1040").into(backIV);
}else{
Picasso.get().load("https://walldorado.com/image/catalog/products/papermoon/fototapet-night-sky-with-moon-(572)-572.jpg").into(backIV);
}
JSONObject forecastOBJ = response.getJSONObject("forecast");
JSONObject forecastO = forecastOBJ.getJSONArray("forecastday").getJSONObject(0);
JSONArray hourArray = forecastO.getJSONArray("hour");
for(int i = 0; i<hourArray.length();i++){
JSONObject hourObj = hourArray.getJSONObject(i);
String time = hourObj.getString("time");
String temp = hourObj.getString("temp_c");
String img = hourObj.getJSONObject("condition").getString("icon");
String wind = hourObj.getString("wind_kph");
weatherRVModelArrayList.add(new WeatherRVModel(time,temp,img,wind));
}
weatherRVAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"please enter valid city name",Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
