Click here to Skip to main content
14,836,857 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
package com.example.JSon_Forecast;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

private static String url = ",88.879.json";

private static final String TAG_FORECAST = "forecast";
private static final String TAG_TXT_FORECAST = "txt_forecast";
private static final String TAG_DATE = "date";
private static final String TAG_FORECASTDAY = "forecastday";
private static final String TAG_PERIOD = "period";
private static final String TAG_ICON = "icon";
private static final String TAG_TITLE = "title";

JSONArray forecast_arr = null;

String date,period,icon,title;

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

ArrayList<hashmap><string,>> list = new ArrayList<hashmap><string,>>();

JSONParser jParser = new JSONParser();

JSONObject jobject = jParser.getJSONFromUrl(url);

try {
JSONObject forecast = jobject
JSONObject txt_forecast = forecast.getJSONObject(TAG_DATE);
date = txt_forecast.getString(TAG_DATE);

TextView txtdate = (TextView) findViewById(;

JSONObject txt_forecast1 = forecast.getJSONObject(TAG_TXT_FORECAST);
JSONObject forecastday1 = txt_forecast1.getJSONObject(TAG_FORECASTDAY);

forecast_arr = forecastday1.getJSONArray("forecastday");

for(int i = 0; i < forecast_arr.length(); i++){
JSONObject ob = forecast_arr.getJSONObject(i);

period = ob.getString(TAG_PERIOD);
icon = ob.getString(TAG_ICON);
title = ob.getString(TAG_TITLE);

HashMap<string,> map = new HashMap<string,>();

map.put(TAG_PERIOD, period);
map.put(TAG_ICON, icon);
map.put(TAG_TITLE, title);


catch (JSONException e) {

ListAdapter adapter = new SimpleAdapter(this, list,
new String[] { TAG_PERIOD, TAG_ICON, TAG_TITLE }, new int[] {,, });

setListAdapter(adapter); //but getting error here



//and the json viewer shows the elements as

"forecast": {
"txt_forecast": {
"date": "5:30 AM IST",
"forecastday": [
"period": 0,
"icon": "clear",
"icon_url": "",
"title": "Monday",
"fcttext": "Clear. High of 86F. Winds from the North at 5 to 10 mph.",
"fcttext_metric": "Clear. High of 30C. Winds from the North at 10 to 15 km/h.",
"pop": "0"
"period": 1,
"icon": "clear",
"icon_url": "",
"title": "Monday Night",
"fcttext": "Clear. Low of 66F. Winds from the SSW at 5 to 10 mph shifting to the NW after midnight.",
"fcttext_metric": "Clear. Low of 19C. Winds from the SSW at 10 to 15 km/h shifting to the NW after midnight.",
"pop": "0"
Sergey Alexandrovich Kryukov 24-Feb-14 12:21pm
Not clear. Where do you have some trouble?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900