-1

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

Emil
  • 2,715
  • 18
  • 24
ManishNegi
  • 557
  • 1
  • 6
  • 17

3 Answers3

2

To set image for imageView try this icon.setImageResource(R.drawable.icons);

0

At getResources().getDrawable(resource) you should use a resource identifier, as generated by the aapt tool (like R.drawable.app_icon), which easier to define by the R resource. And this method also depraceated, so I recommend to use:

ContextCompat.getDrawable(getApplicationContext(), R.drawable.[your_image_path]) like in this post

or icon.setImageResource(R.drawable.[your_image_path])

Community
  • 1
  • 1
bendaf
  • 2,862
  • 5
  • 23
  • 58
0

You can set the drawable with the name as follows

Resources res = getResources();
String mDrawableName = "your_drawable_name";// i.e. icon
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
icon.setImageDrawable(drawable );
Prasad
  • 3,132
  • 1
  • 19
  • 26
  • Thank you all. I've overcome that problem – ManishNegi Aug 24 '15 at 11:44
  • Now i'm facing a new problem i've json data which i want to use in my app which looks like – ManishNegi Aug 24 '15 at 11:46
  • {"location":{"name":"Delhi","region":"Delhi","country":"India","lat":28.67,"lon":77.22,"tz_id":"Asia/Kolkata","localtime_epoch":1440435969,"localtime":"2015-08-24 17:06"},"current":{"last_updated_epoch":1440435604,"last_updated":"2015-08-24 17:00","temp_c":34.0,"temp_f":93.2,"condition":{"text":"Mist","icon":"http://www.apixu.com/static/weather/64x64/day/143.png","code":1030},"wind_mph":11.9,"wind_kph":19.1,"wind_degree":300,"wind_dir":"WNW","pressure_mb":1002.0,"pressure_in":30.1,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":42.2,"feelslike_f":108.0}} – ManishNegi Aug 24 '15 at 11:46
  • here i'm not able to distinguish between jsonobject and jsonarray.. please help me out guys – ManishNegi Aug 24 '15 at 11:47
  • 1
    Simple difference is JsonObject starts with { and JsonArray starts with [ – Prasad Aug 24 '15 at 12:41
  • Okay thank you again.. i've resolved this issue too.. now i've stucked somewhere else.. I need to set an image from a url to an Imageview.... i'm using async task for parsing json. and doInBackground Method returns JSONObject .. Now here is the problem .. How to set the image on the imageview – ManishNegi Aug 24 '15 at 13:37