Click here to Skip to main content
15,867,834 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.app.Activity;
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 = "http://api.wunderground.com/api/356d60036a9374e9/conditions/forecast/alert/q/22.897,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;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_listview);

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

JSONParser jParser = new JSONParser();

JSONObject jobject = jParser.getJSONFromUrl(url);

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

TextView txtdate = (TextView) findViewById(R.id.txt_date);
txtdate.setText(date);

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);

list.add(map);


}
}
catch (JSONException e) {
e.printStackTrace();
}

ListAdapter adapter = new SimpleAdapter(this, list,
R.layout.main,
new String[] { TAG_PERIOD, TAG_ICON, TAG_TITLE }, new int[] {
R.id.txt_period, R.id.txt_icon, R.id.txt_title });

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": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"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": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"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"
},
Posted
Comments
Sergey Alexandrovich Kryukov 24-Feb-14 12:21pm    
Not clear. Where do you have some trouble?
—SA

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