i am trying to make a list view it is a weather application and it is not displaying. seems it is not getting the data like it is empty and i don't know why i am a bit stuck. this is the first class.
i am getting the details from and api and it meant to fetch the data and display. I am getting fatal error in my log cat i don't understand.i really need to do this fast it is important. i don't know if this is the best way to come about it.
this is my search.java
package com.helloworldng.bip.navigate; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.util.Log; import android.os.Bundle; import android.os.Handler; import android.text.format.Time; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import com.helloworldng.bip.GetTrafficReportTask; import com.helloworldng.bip.R; import com.helloworldng.bip.data.BIPContract; public class Search extends ListActivity { private final String LOG_TAG = GetTrafficReportTask.class.getSimpleName(); public static final String BIP_STATE = "state"; public static final String BIP_CITY = "city"; public static final String BIP_DESC = "description"; public static final String BIP_STATUS = "status"; private static final String URL_CATEGORY = "http://www.helloworldng.com/bip/get_traffic.php"; private final Context mContext; //public FetchWeatherTask(Context context, ArrayAdapter<String> forecastAdapter) { public Search(Context context) { mContext = context; //mForecastAdapter = forecastAdapter; } private BaseAdapter mAdapter; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.droptrafic); lv = getListView(); lv.setDivider(null); final Handler handler = new Handler(); Runnable refresh = new Runnable() { @Override public void run() { handler.postDelayed(this, 60 * 1000); } }; handler.postDelayed(refresh, 60 * 1000); } private void getTrafficDataFromJson(String forecastJsonStr) throws JSONException { try { JSONArray trafficArray = new JSONArray(forecastJsonStr); //JSONArray trafficArray = forecastJson.getJSONArray(BIP_TRAFFIC); // Insert the new weather information into the database Vector<ContentValues> cVVector = new Vector<ContentValues>(trafficArray.length()); Time dayTime = new Time(); dayTime.setToNow(); dayTime = new Time(); for (int i = 0; i < trafficArray.length(); i++) { String state; String status; JSONObject dayTraffic = trafficArray.getJSONObject(i); state = dayTraffic.getString(BIP_STATE); status = dayTraffic.getString(BIP_STATUS); final ListAdapter adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) dayTraffic, R.layout.single_drop, new String[] { state,status }, new int[] { R.id.title, R.id.desc}); setListAdapter(adapter); ListView lv = getListView(); } } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } //return null; } protected Void doInBackground(String... params) { // If there's no zip code, there's nothing to look up. Verify size of params. if (params.length == 0) { return null; } String locationQuery = params[0]; // These two need to be declared outside the try/catch // so that they can be closed in the finally block. HttpURLConnection urlConnection = null; BufferedReader reader = null; // Will contain the raw JSON response as a string. String forecastJsonStr = null; String format = "json"; String units = "metric"; int numDays = 7; String app_id = "b7940b102bb44bb3c5accd21eb44da00"; try { // Construct the URL for the OpenWeatherMap query // Possible parameters are avaiable at OWM's forecast API page, at // http://openweathermap.org/API#forecast final String FORECAST_BASE_URL = "http://www.helloworldng.com/bip/get_traffic.php"; final String QUERY_PARAM = "q"; final String FORMAT_PARAM = "mode"; final String UNITS_PARAM = "units"; final String DAYS_PARAM = "cnt"; final String APPID = "appid"; Uri builtUri = Uri.parse(FORECAST_BASE_URL); URL url = new URL(builtUri.toString()); Log.e("Today Traffic", url.toString()); // Create the request to OpenWeatherMap, and open the connection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); // Read the input stream into a String InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); if (inputStream == null) { // Nothing to do. return null; } reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) // But it does make debugging a *lot* easier if you print out the completed // buffer for debugging. buffer.append(line + "\n"); } if (buffer.length() == 0) { // Stream was empty. No point in parsing. return null; } forecastJsonStr = buffer.toString(); Log.e(LOG_TAG + " Manual", forecastJsonStr); getTrafficDataFromJson(forecastJsonStr); } catch (IOException e) { Log.e(LOG_TAG, "Error ", e); // If the code didn't successfully get the weather data, there's no point in attempting // to parse it. // return null; } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final IOException e) { Log.e(LOG_TAG, "Error closing stream", e); } } } return null; } }
No comments:
Post a Comment