0

I want to take String data in ArrayList. I attempted to retrieve forecast data (which is in JSON format) from OpenWeatherMap API. "I stored data in JSONObject,then take it in string. Now I want to show data with help of ArrayList,But I am not able to retrive data in ArayList. I had debug code with LogCat and found that all the String have data but ArrayList is not taking data." Can anyone tell me where i am doing wrong.Below is my code of OnpostExecute method. Thanks for any help.

protected void onPostExecute(String file_url) {

    String gradfix = city.replace("%20", " ");
    grad.setText(gradfix);
    pb.setVisibility(View.GONE);
    grad.setVisibility(View.VISIBLE);

    try {
        // Getting Array of Contacts
        JSONObject json2 = new JSONObject(file_url);
        JSONArray forecast = null;
        forecast = json2.getJSONArray("list");

        // looping through All Contacts
        for(int i = 0; i < forecast.length(); i++){

            JSONObject c = forecast.getJSONObject(i);
            String dt = c.getString("dt");          
            String day = c.getString("day");
            Log.e("day",day.toString());
            String min = c.getString("min");
            String max = c.getString("max");
            String pressure = c.getString("pressure");
            String humidity = c.getString("humidity");

            JSONArray weather = c.getJSONArray("weather");

            for(int index = 0; index < weather.length(); index++){
                JSONObject weatherObject = weather.getJSONObject(index);
                String clouds = weatherObject.getString("clouds");
                String speed = weatherObject.getString("speed");
                String deg = weatherObject.getString("deg");
            }

        }

        final ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        for (HashMap<String, Object> map : list) {

            //HashMap<String, Object> map = new HashMap<String, Object>();
            //Log.e("list",pressure.toString());
            double srednja = Double.parseDouble(map.get("day").toString());
            double ssrednja2 = (double) Math.floor(srednja);
            srednja1.setText(format.format(ssrednja2)+"°F");
            tlak.setText(map.get("pressure").toString()+" hpa");
            tlak.setVisibility(View.VISIBLE);
            vlaznost.setText(map.get("humidity").toString()+" %");
            vlaznost.setVisibility(View.VISIBLE);
            vjetar.setText(map.get("speed").toString()+" m/s");
            vjetar.setVisibility(View.VISIBLE);
            String imgrestring = map.get("icon").toString();
            Bitmap slika = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("img"+imgrestring, "drawable", getPackageName()));
            img_mid.setImageBitmap(slika);
            Float stupnjevi = Float.valueOf(map.get("deg").toString());
            Log.d("jea", String.valueOf(stupnjevi));
            Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), R.drawable.wind);
            Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas tempCanvas = new Canvas(bmResult); 
            tempCanvas.rotate(stupnjevi, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
            tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
            BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bmResult);
            vjetar.setCompoundDrawablesWithIntrinsicBounds( null, null, mDrawable, null);
            stanje.setText(map.get("main").toString());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
9
  • 3
    for (HashMap<String, Object> map : list) { you are iterating on a list that you just created. it is empty. what else did you expect? Commented Oct 27, 2016 at 15:26
  • The best option would be to deserialize the JSON data into some custom Java class. Or, in your current approach, you could do what you need inside the weather.length() loop - because here you iterate through the actual weather information object. Commented Oct 27, 2016 at 15:29
  • Your ArrayList<HashMap<String, Object>> list is empty, only initialized, therefore you will never enter in for loop. Commented Oct 27, 2016 at 15:29
  • @njzk2 Yeah value of list is null. I want to know, how to insert values in list from string variable (dt,day,min,max etc). I am missing some syntax there. Commented Oct 27, 2016 at 15:35
  • You need to put things in your list. You are iterating on your JSON objects, but you are not doing anything with what you are reading. Put those values in maps and in your list. Commented Oct 27, 2016 at 15:40

1 Answer 1

1

Since it is too long for comment, I will put this example in answer:

Example:

JSONObject weatherObject;
String clouds;
String speed;
String deg;
String id;

MyWeatherObject myWeatherObject;

final ArrayList<HashMap<String, MyWeatherObject>> list = new ArrayList<HashMap<String, MyWeatherObject>>();
HashMap<String, MyWeatherObject> map;

for (int index = 0; index < weather.length(); index++) {
    weatherObject = weather.getJSONObject(index);
    clouds = weatherObject.getString("clouds");
    speed = weatherObject.getString("speed");
    deg = weatherObject.getString("deg");
    myWeatherObject = new MyWeatherObject();

    myWeatherObject.setClouds(clouds);
    myWeatherObject.setSpeed(speed);
    myWeatherObject.setDeg(deg);

    // some unique String/Integer/... which is kind of ID for weatherObject
    id = weatherObject.getString("id");

    map = new HashMap<String, MyWeatherObject>();

    map.put(id, myWeatherObject);

    // then, if you really want to put it in the array list, do it here
    list.add(map);
    // you can always store your data only in map, but then your map should not be initialized inside for loop
    // most likely you don't need an array list of the maps, but that I cannot see that from your example. 
    // Seems like you need only a hash map or an array list of pairs or an array list of MyWeatherObjects.

}

// your list is not empty anymore if your JSONArray/JSONObject is not empty
for (HashMap<String, MyWeatherObject> map : list) {
    // here you can reach your MyWeatherObject and there you have getClouds, getSpeed, getDeg...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.