Android Open Source - WeatherMate Flight Tracking Parser






From Project

Back to project page WeatherMate.

License

The source code is released under:

Apache License

If you think the Android project WeatherMate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.brightr.weathermate.parsers;
//  w ww  . ja va  2s  .com
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Build;
import android.util.Log;

//Class to retrieve the flight tracking data and parse the returned JSON

public class FlightTrackingParser {
  
  private static final String API_KEY = "50ba19799c05dfa32b320f1ed0320decf7d5383d";
  private static final String userName = "jjbyfield";
  private static final int QUERY_FLIGHTINFO = 0;
  private static final int QUERY_FLIGHTDETAILS = 1;
  
  
//Input stream that will read the data
     static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
      
      
      // constructor
      public FlightTrackingParser() {
   
      }
      
      
      public static JSONObject getJSONFromUrl(String query, String maxResults) {
        
      String url = "http://flightxml.flightaware.com/json/FlightXML2/FlightInfoEx?ident="+query+"&howMany="+maxResults+"&offset=0";
         
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpGet httpGet = new HttpGet(url);
             // HttpPost httpPost = new HttpPost(url);
              
              httpGet.addHeader(BasicScheme.authenticate(
                   new UsernamePasswordCredentials(userName, API_KEY),
                   "UTF-8", false));
              
              
   
              HttpResponse httpResponse = httpClient.execute(httpGet);
              
             
              HttpEntity httpEntity = httpResponse.getEntity();
              
              is = httpEntity.getContent();          
   
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
   
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
              Log.w("FlightTrackingParser", json.toString());
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
   
          // try parsing the string to a JSON object
          try {
            if(json != null){
              jObj = new JSONObject(json);
            }else{
              jObj = null;
            }
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
   
          // return JSON String
          return jObj;
         
   
      }
      
      
      
      
      public static JSONObject getFlightDetails(String flightId) {
        
        String url = "http://flightxml.flightaware.com/json/FlightXML2/AirlineFlightInfo?faFlightID=" + flightId;
           
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
               // HttpPost httpPost = new HttpPost(url);
                
                httpGet.addHeader(BasicScheme.authenticate(
                     new UsernamePasswordCredentials(userName, API_KEY),
                     "UTF-8", false));
                
                
     
                HttpResponse httpResponse = httpClient.execute(httpGet);
                
               
                HttpEntity httpEntity = httpResponse.getEntity();
                
                is = httpEntity.getContent();          
     
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
                Log.w("FlightTrackingParser", json.toString());
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
     
            // try parsing the string to a JSON object
            try {
              if(json != null){
                jObj = new JSONObject(json);
              }else{
                jObj = null;
              }
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
     
            // return JSON String
            return jObj;
           
     
        }
        
        
        
      
      
      
      

}




Java Source Code List

.PullToRefreshListView.java
com.brightr.listeners.FlingGestureListener.java
com.brightr.listeners.OnFlingGestureListener.java
com.brightr.listeners.SimpleGestureFilter.java
com.brightr.weathermate.activities.LocationMapview.java
com.brightr.weathermate.activities.MainActivity.java
com.brightr.weathermate.activities.MyFlightsActivity.java
com.brightr.weathermate.activities.NewsViewerActivity.java
com.brightr.weathermate.activities.SearchLocationsActivity.java
com.brightr.weathermate.activities.SettingsActivity.java
com.brightr.weathermate.activities.SocialMediaActivity.java
com.brightr.weathermate.activities.WebsiteViewActivity.java
com.brightr.weathermate.adapters.FlightListAdapter.java
com.brightr.weathermate.adapters.LocationListAdapter.java
com.brightr.weathermate.adapters.MenuAdapter.java
com.brightr.weathermate.adapters.MyPagerAdapter.java
com.brightr.weathermate.adapters.NewsPagerAdapter.java
com.brightr.weathermate.adapters.SavedFlightsAdapter.java
com.brightr.weathermate.adapters.SearchListAdapter.java
com.brightr.weathermate.adapters.TweetAdapter.java
com.brightr.weathermate.adapters.WeatherAdapter.java
com.brightr.weathermate.databases.FlightStorage.java
com.brightr.weathermate.databases.LocationStorage.java
com.brightr.weathermate.databases.NewsStorage.java
com.brightr.weathermate.fragments.EntertainmentNewsFragment.java
com.brightr.weathermate.fragments.GeneralNewsFragment.java
com.brightr.weathermate.fragments.MainWeatherFragment.java
com.brightr.weathermate.fragments.MenuFragment.java
com.brightr.weathermate.fragments.PoliticalNewsFragment.java
com.brightr.weathermate.fragments.SidePanelFragment.java
com.brightr.weathermate.fragments.SportsNewsFragment.java
com.brightr.weathermate.parsers.FlightTrackingParser.java
com.brightr.weathermate.parsers.JSONParser.java
com.brightr.weathermate.parsers.JSONTrafficParser.java
com.brightr.weathermate.parsers.TwitterFeedParser.java
com.brightr.weathermate.providers.FlightTrackingProvider.java
com.brightr.weathermate.providers.LocationSearchProvider.java
com.brightr.weathermate.providers.TrafficIncidentProvider.java
com.brightr.weathermate.providers.WeatherProvider.java
com.brightr.weathermate.utils.AirlineNamesCollection.java
com.brightr.weathermate.views.CustomDialogPreference.java
com.brightr.weathermate.views.CustomPreferenceCategory.java