Android Open Source - Flight-Computer-Android-Flightradar24 Net Utils






From Project

Back to project page Flight-Computer-Android-Flightradar24.

License

The source code is released under:

GNU General Public License

If you think the Android project Flight-Computer-Android-Flightradar24 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.nutiteq.utils;
//from  ww  w .j a va 2 s .  co  m
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;

import com.nutiteq.log.Log;

public class NetUtils {
    public static String downloadUrl(String url, Map<String, String> httpHeaders, boolean gzip, String encoding) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            
            if(gzip){
                AndroidHttpClient.modifyRequestToAcceptGzipResponse(request);
            }

            if(httpHeaders != null){
                for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
                    request.addHeader(entry.getKey(), entry.getValue());
                }
              }
            
            HttpResponse response = client.execute(request);
            InputStream ips;
            if(gzip){
                ips  = AndroidHttpClient
                        .getUngzippedContent(response.getEntity());
            }else{
                ips  = new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity()));
            }
            
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips,encoding));

            StringBuilder sb = new StringBuilder();
            String s;

            while ((s = buf.readLine()) != null) {
                sb.append(s);
            }
            
            buf.close();
            ips.close();
            Log.debug("loaded: "+sb.toString());
            return sb.toString();

            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        return null;
    }


    /**
     * HTTP Post data
     * @param url
     * @param httpHeaders
     * @param gzip
     * @param postData
     * @return
     */
    public static String postUrl(String url, Map<String, String> httpHeaders, boolean gzip, HttpEntity postData, String encoding ) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost();
            request.setURI(new URI(url));
            Log.debug("POST to "+url);
            if(gzip){
                AndroidHttpClient.modifyRequestToAcceptGzipResponse(request);
            }

            if(httpHeaders != null){
                for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
                    request.addHeader(entry.getKey(), entry.getValue());
                }
              }
            
            request.setEntity(postData);
            
            HttpResponse response = client.execute(request);
            InputStream ips;
            if(gzip){
                ips  = AndroidHttpClient
                        .getUngzippedContent(response.getEntity());
            }else{
                ips  = new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity()));
            }
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips, encoding));

            StringBuilder sb = new StringBuilder();
            String s;

            while ((s = buf.readLine()) != null) {
                sb.append(s);
            }
            
            buf.close();
            ips.close();
            Log.debug("loaded: "+sb.toString());
            return sb.toString();
            
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        return null;
    }

    
    /**
     * Helper method to load JSON 
     */
    
    public static JSONObject getJSONFromUrl(String url) {

        InputStream is = null;
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpRequest = new HttpGet(url);

            AndroidHttpClient.modifyRequestToAcceptGzipResponse(httpRequest);

            HttpResponse httpResponse = httpClient.execute(httpRequest);
            is = AndroidHttpClient
                    .getUngzippedContent(httpResponse.getEntity());

            String json = null;
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.error("Error converting result " + e.toString());
            }

            JSONObject jObj = null;
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.error("Error parsing data " + e.toString());
            }

            // return JSON String
            return jObj;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    
    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
}




Java Source Code List

com.flightcomputer.Circle.java
com.flightcomputer.DownloadActivity.java
com.flightcomputer.EditPreferences.java
com.flightcomputer.FCActivity.java
com.flightcomputer.GpsSkyActivity.java
com.flightcomputer.IgcLoad.java
com.flightcomputer.LeonardoLiveWriter.java
com.flightcomputer.MapEventListener.java
com.flightcomputer.PositionWriter.java
com.flightcomputer.PressureAltimeterActivity.java
com.flightcomputer.RouteLine.java
com.flightcomputer.Task.java
com.flightcomputer.TrckLine.java
com.flightcomputer.filefilter.FilterByFileExtension.java
com.flightcomputer.filefilter.ValidFileFilter.java
com.flightcomputer.filefilter.ValidMapFile.java
com.flightcomputer.filefilter.ValidRenderTheme.java
com.flightcomputer.filepicker.FilePickerIconAdapter.java
com.flightcomputer.filepicker.FilePicker.java
com.flightcomputer.utilities.BaroService.java
com.flightcomputer.utilities.BeepThread.java
com.flightcomputer.utilities.GpsSkyView.java
com.flightcomputer.utilities.KalmanFilter.java
com.flightcomputer.utilities.PiecewiseLinearFunction.java
com.flightcomputer.utilities.Point2d.java
com.flightcomputer.utilities.VerticalProgressBar_Reverse.java
com.flightcomputer.utilities.VerticalProgressBar.java
com.flightcomputer.openGlUtils.java
com.nutiteq.advancedmap.mapquest.SearchQueryResults.java
com.nutiteq.advancedmap.mapquest.SearchSuggestionProvider.java
com.nutiteq.services.geocode.MapQuestGeocoder.java
com.nutiteq.utils.MapsforgeMapLayer.java
com.nutiteq.utils.MbTilesDatabaseHelper.java
com.nutiteq.utils.MyLocationCircle.java
com.nutiteq.utils.NetUtils.java
com.nutiteq.utils.UiUtils.java
com.nutiteq.utils.UtfGridHelper.java
com.nutiteq.utils.UtfGridLayerInterface.java