Hello all I'm using a weather API to get weather data
public class Weather extends Activity {
String city;
public TextView t,tmin,tmax;
ImageView icon;
String img;
private static final String API_URL = "http://api.openweathermap.org/data/2.5/weather?q=";
private static final String MAIN = "main";
private static final String TEMP = "temp";
private static final String TEMP_MIN = "pressure";
private static final String TEMP_MAX = "temp_max";
JSONObject data = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
t = (TextView) findViewById(R.id.temp);
tmin = (TextView) findViewById(R.id.minTemp);
tmax = (TextView) findViewById(R.id.maxTemp);
icon=(ImageView)findViewById(R.id.imageView);
city = getIntent().getExtras().getString("city_name");
new JSONParse().execute();
}
@Override
public void onBackPressed() {
super.onBackPressed();
t.setText("N/A");
tmin.setText("N/A");
tmax.setText("N/A");
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog progressDialog;
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Weather.this);
progressDialog.setMessage("Getting Data...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected JSONObject doInBackground(String... strings) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(API_URL + city);
return json;
}
protected void onPostExecute(JSONObject json) {
progressDialog.dismiss();
try {
JSONArray arr=null;
data = json.getJSONObject(MAIN);
arr=json.getJSONArray("weather");
JSONObject obj=arr.getJSONObject(0);
String conditon=obj.getString("description");
double tmp = data.getDouble(TEMP);
double tmn = data.getDouble(TEMP_MIN);
double tmx = data.getDouble(TEMP_MAX);
img=obj.getString("icon");
// int resource=getResources().getIdentifier("@drawable/icons/"+img,null,null);
// Drawable drw=getResources().getDrawable(resource);
// icon.setImageDrawable(drw);
t.setText("" +Math.floor(tmp-273.15)+"°C");
tmax.setText("" + img);
tmin.setText("" + conditon);
} catch (Exception e) {
Log.i("Inside Weather :",""+e);
e.printStackTrace();
}
The part which i've commented is not working please suggest a solution. Thanks