Skip to content

Commit

Permalink
add Accept-Encoding: gzip to fetch xml data request to speed up fetch…
Browse files Browse the repository at this point in the history
…ing by ~12x
  • Loading branch information
shlusiak committed Sep 28, 2012
1 parent 236700c commit 33fc473
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/de/saschahlusiak/hrw/dienststatus/model/Dienststatus.java
@@ -1,11 +1,14 @@
package de.saschahlusiak.hrw.dienststatus.model;

import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -23,8 +26,7 @@

public class Dienststatus {
private static final String tag = Dienststatus.class.getSimpleName();
private static final HttpUriRequest uri = new HttpGet(
"http://nagvis-pub.hs-weingarten.de/cgi-bin/nagxml.pl?all");
private static final String uri = "http://nagvis-pub.hs-weingarten.de/cgi-bin/nagxml.pl?all";

private static Document dom = null;
private static ArrayList<HRWNode> allnodes = new ArrayList<HRWNode>();
Expand Down Expand Up @@ -107,9 +109,12 @@ private static void parseLevel(Node item, HRWNode HRWparent) {
public synchronized static String fetch(Context context) {
try {
DefaultHttpClient client = new DefaultHttpClient();
final HttpResponse resp = client.execute(uri);
HttpUriRequest req = new HttpGet(uri);
req.addHeader("Accept-Encoding", "gzip");

final StatusLine status = resp.getStatusLine();
HttpResponse resp = client.execute(req);

StatusLine status = resp.getStatusLine();
if (status.getStatusCode() != 200) {
Log.d(tag,
"HTTP error, invalid server status code: "
Expand All @@ -122,7 +127,13 @@ public synchronized static String fetch(Context context) {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
dom = builder.parse(resp.getEntity().getContent());

InputStream is = resp.getEntity().getContent();
Header contentEncoding = resp.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(is);
}
dom = builder.parse(is);
} catch (UnknownHostException e) {
Log.e(tag, e.getMessage());
return "Unknown host: " + e.getMessage();
Expand Down

0 comments on commit 33fc473

Please sign in to comment.