Android Open Source - radioExpresWidget My Http Client






From Project

Back to project page radioExpresWidget.

License

The source code is released under:

GNU General Public License

If you think the Android project radioExpresWidget 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.hustaty.radioexpres.widget.http;
/*from   w  ww.  j a  va2 s.  c  o  m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.location.Location;
import android.os.StrictMode;
import android.util.Log;

import com.hustaty.radioexpres.widget.exception.RadioExpresException;
import com.hustaty.radioexpres.widget.model.Kml;
import com.hustaty.radioexpres.widget.service.LocationService;
import com.hustaty.radioexpres.widget.util.LogUtil;
import com.hustaty.radioexpres.widget.util.Serializer;

public class MyHttpClient extends DefaultHttpClient {

  // logger entry
  private final static String LOG_TAG = MyHttpClient.class.getName();

  private final Context context;

  public MyHttpClient(Context context) {
    this.context = context;
  }

  public Kml getTrafficInformation(boolean shutdownAfterGettingInfo) throws IOException, RadioExpresException {

    Location location = LocationService.obtainCurrentLocation(context);

    if (android.os.Build.VERSION.SDK_INT > 9) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
    }

    if (this.getConnectionManager() == null) {
      this.createClientConnectionManager();
    }

    HttpGet httpGet = new HttpGet("http://eds.expres.sk/kml/?");

    httpGet.addHeader("Host", "eds.expres.sk");
    httpGet.addHeader("User-Agent", "Hustaty Radio Expres Android Client");
    httpGet.addHeader("Connection", "Keep-Alive");

    HttpResponse response = this.execute(httpGet);

    String result = httpResponseText(response);
    Kml kmlResult = null;
    LogUtil.appendLog(result);

    Serializer kmlSerializer = new Serializer();

    try {
      kmlResult = kmlSerializer.read(result);
    } catch (Exception e) {
      LogUtil.appendLog(LOG_TAG + "#getTrafficInformation(): " + result);
      Log.e(LOG_TAG + "#getTrafficInformation(): ", e.getMessage());
    }

    if (shutdownAfterGettingInfo) {
      shutdownConnectionManager();
    }

    return kmlResult;

  }

  private void shutdownConnectionManager() {
    this.getConnectionManager().shutdown();
  }

  /**
   * Parse response to text.
   * 
   * @param response
   * @return
   * @throws IOException
   */
  private String httpResponseText(HttpResponse response) throws IOException {

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    String responseText = "";
    String line = "";
    while ((line = rd.readLine()) != null) {
      responseText += line;
    }

    return responseText;
  }
}




Java Source Code List

com.hustaty.radioexpres.widget.MyActivity.java
com.hustaty.radioexpres.widget.exception.RadioExpresException.java
com.hustaty.radioexpres.widget.http.MyHttpClient.java
com.hustaty.radioexpres.widget.model.Document.java
com.hustaty.radioexpres.widget.model.IconStyle.java
com.hustaty.radioexpres.widget.model.Icon.java
com.hustaty.radioexpres.widget.model.Kml.java
com.hustaty.radioexpres.widget.model.Placemark.java
com.hustaty.radioexpres.widget.model.Point.java
com.hustaty.radioexpres.widget.model.Style.java
com.hustaty.radioexpres.widget.receiver.AlarmManagerBroadcastReceiver.java
com.hustaty.radioexpres.widget.service.LocationService.java
com.hustaty.radioexpres.widget.util.Constants.java
com.hustaty.radioexpres.widget.util.LogUtil.java
com.hustaty.radioexpres.widget.util.Serializer.java