Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move the common code out into its own project.

  • Loading branch information...
commit abcc96c0c480bdcf79b01aad684628d4ba82f4d8 1 parent 4bbc9fa
@leenookx authored
View
1  .classpath
@@ -6,5 +6,6 @@
<classpathentry kind="lib" path="lib/acra-3.1.0.jar"/>
<classpathentry kind="lib" path="lib/jdom.jar"/>
<classpathentry kind="lib" path="lib/rome-1.0.jar"/>
+ <classpathentry kind="src" path="/PurpleMonkeyCommons"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
37 src/uk/co/purplemonkeys/common/Common.java
@@ -1,37 +0,0 @@
-package uk.co.purplemonkeys.common;
-
-import android.app.AlertDialog;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.widget.Toast;
-
-public class Common
-{
- public static void ShowErrorToast(Context context, String error, int duration)
- {
- Toast toast = Toast.makeText(context, error, duration);
- toast.setDuration( duration );
- toast.show();
- }
-
- public static void ShowAlertMessage(Context c, String message)
- {
- // Create the alert box
- AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
-
- // Set the message to display
- alertbox.setMessage( message );
-
- // Add a neutral button to the alert box and assign a click listener
- alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- // We don't really care about this here...
- }
- });
-
- // show the alert box
- alertbox.show();
- }
-}
View
79 src/uk/co/purplemonkeys/common/XmlResponseParser.java
@@ -1,79 +0,0 @@
-package uk.co.purplemonkeys.common;
-
-import java.io.IOException;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.protocol.HTTP;
-import org.apache.http.util.EntityUtils;
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlPullParserFactory;
-
-import android.util.Log;
-
-/**
- * Contains all parser creation and exception handling logic, for parsers
- * that consume an HttpEntity stream and produce a single object as a result.
- *
- * XXX - does not provide a way to return a result before input is completely consumed
- *
- * @author spapadim
- *
- */
-public abstract class XmlResponseParser<T>
-{
- private final String TAG = "XmlResponseParser";
- private HttpEntity e;
- private T result;
-
- private XmlPullParserFactory mXmlParserFactory;
-
- public XmlResponseParser (HttpEntity e) { this.e = e; }
-
- public void setResult (T result) { this.result = result; }
- public T getResult () { return result; }
-
- /**
- * Handle an XML event from the parser.
- * @return False if parsing is complete, true otherwise
- */
- abstract public boolean onXmlEvent (XmlPullParser parser, int eventType) throws XmlPullParserException, IOException;
-
- public void onInit (XmlPullParser parser) { }
-
- public T parse () {
- try {
- XmlPullParser parser = getXmlParser(e);
- onInit(parser);
- int eventType = parser.getEventType();
- while (eventType != XmlPullParser.END_DOCUMENT) {
- if (!onXmlEvent(parser, eventType)) {
- break;
- }
- eventType = parser.next();
- }
- } catch (Throwable t) {
- Log.e(TAG, "XmlResponseParser exception", t);
- setResult(null);
- } finally {
- try {
- e.consumeContent();
- } catch (Throwable t2) { }
- }
- return getResult();
- }
-
- private final XmlPullParser getXmlParser (HttpEntity e) throws XmlPullParserException, IllegalStateException, IOException
- {
- if (mXmlParserFactory == null) {
- mXmlParserFactory = XmlPullParserFactory.newInstance();
- }
- XmlPullParser parser = mXmlParserFactory.newPullParser();
- String charSet = EntityUtils.getContentCharSet(e);
- if (charSet == null) {
- charSet = HTTP.DEFAULT_CONTENT_CHARSET;
- }
- parser.setInput(e.getContent(), charSet);
- return parser;
- }
-}
View
31 src/uk/co/purplemonkeys/common/http/GzipDecompressingEntity.java
@@ -1,31 +0,0 @@
-package uk.co.purplemonkeys.common.http;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.zip.GZIPInputStream;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.entity.HttpEntityWrapper;
-
-public class GzipDecompressingEntity extends HttpEntityWrapper
-{
- public GzipDecompressingEntity(final HttpEntity entity)
- {
- super(entity);
- }
-
- @Override
- public InputStream getContent() throws IOException, IllegalStateException
- {
- // the wrapped entity's getContent() decides about repeatability
- InputStream wrappedin = wrappedEntity.getContent();
- return new GZIPInputStream(wrappedin);
- }
-
- @Override
- public long getContentLength()
- {
- // length of ungzipped content is not known
- return -1;
- }
-}
View
94 src/uk/co/purplemonkeys/common/http/HttpCommon.java
@@ -1,94 +0,0 @@
-package uk.co.purplemonkeys.common.http;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-
-import org.apache.http.Header;
-import org.apache.http.HeaderElement;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpException;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.scheme.PlainSocketFactory;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.protocol.HttpContext;
-
-
-public class HttpCommon
-{
-
- public static DefaultHttpClient createGzipHttpClient()
- {
- BasicHttpParams params = new BasicHttpParams();
- SchemeRegistry schemeRegistry = new SchemeRegistry();
- schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
-
- DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
-
- httpclient.addRequestInterceptor(new HttpRequestInterceptor()
- {
- public void process(final HttpRequest request,
- final HttpContext context) throws HttpException, IOException {
- if (!request.containsHeader("Accept-Encoding")) {
- request.addHeader("Accept-Encoding", "gzip");
- }
- }
- });
-
- httpclient.addResponseInterceptor(new HttpResponseInterceptor()
- {
- public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException
- {
- HttpEntity entity = response.getEntity();
- Header ceheader = entity.getContentEncoding();
- if (ceheader != null)
- {
- HeaderElement[] codecs = ceheader.getElements();
- for (int i = 0; i < codecs.length; i++)
- {
- if (codecs[i].getName().equalsIgnoreCase("gzip"))
- {
- response.setEntity(new GzipDecompressingEntity(response.getEntity()));
- return;
- }
- }
- }
- }
- });
-
- return httpclient;
- }
-
- public static StringBuilder convertStreamToString(InputStream is) throws IOException{
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"ISO-8859-1"));
- StringBuilder sb = new StringBuilder();
-
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
-
- sb.append(line + "\n");
- }
- } catch (IOException e) {
- throw e;
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- throw e;
- }
- }
- return sb;
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.