Skip to content

Instantly share code, notes, and snippets.

View rsaunders100's full-sized avatar

Rob Saunders rsaunders100

View GitHub Profile
@rsaunders100
rsaunders100 / printStackTraceWithTag.java
Created September 7, 2011 15:42
(Android) Prints a stack trace of an exception with a given tag.
private static void printStackTraceWithTag(Exception e, String tag)
{
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
Log.e(tag,stringWriter.toString());
}
@rsaunders100
rsaunders100 / ExampleAsyncTask.java
Created September 7, 2011 15:54
(Android) The basic structure of Android's Async task.
public class ExampleAsyncTask extends AsyncTask<Void, Void, ResultObject>
{
private final String _inputParameter;
// Constuctor
public LogInTask(String _inputParameter)
{
_inputParameter = inputParameter;
}
@rsaunders100
rsaunders100 / ExampleAdapter.java
Created September 7, 2011 16:01
(Android) Example code for a simple array adaptor.
public class ExampleAdapter extends ArrayAdapter<String> {
private LayoutInflater _inflater;
//
// Initialize the adaptor with an Array List of items to display.
//
public ExampleAdapter(Context context, ArrayList<String> strings)
{
@rsaunders100
rsaunders100 / XMLPullParserExample.java
Created September 7, 2011 16:08
(Android) An simple example use of a XML pull parser.
public String parseInputStream(InputStream inputStream) throws Exception
{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType){
case XmlPullParser.START_DOCUMENT:
@rsaunders100
rsaunders100 / gsonExample.java
Created September 7, 2011 16:14
(Android) Example use of the excelent Gson library used to parse JSON.
// .jar from:
// http://code.google.com/p/google-gson/
import java.util.List;
import com.google.gson.Gson;
public class Test {
public static void main(String... args) throws Exception {
String json =
@rsaunders100
rsaunders100 / SimpleTimerThreshold.java
Created September 7, 2011 17:13
(Android) Use to check check if a threshold has expired since a last event. e.g. 5 mins has expired since the lat time some data has been downloaded. Optional persistant storage.
package uk.co.digitaljigsaw.utils;
import java.util.Calendar;
import java.util.Date;
import android.R.string;
import android.content.Context;
import android.content.SharedPreferences;
@rsaunders100
rsaunders100 / CallUsingDialier.java
Created September 9, 2011 08:28
(Android) Makes a call using the dialier on Android.
Uri uri = Uri.parse("tel:4444");
Intent callIntent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(callIntent);
@rsaunders100
rsaunders100 / GJSONExample2.java
Created September 9, 2011 08:35
(Android) Another example GSON usage for parsing the JSON result of the google Geocoder
@Override
public GeoLocationResult getDataObject(InputStream inputStream) throws Exception {
// Convert to string
String responseString = IOUtils.toString(inputStream, "UTF-8");
// One line parsing FTW!
GeoLocationResult geoLocationResult = new Gson().fromJson(responseString, GeoLocationResult.class);
// Example to get the location latitude
@rsaunders100
rsaunders100 / hackFeed.m
Created September 9, 2011 16:14
(iOS) Saves the response of a parsing result to disk and restores it later, for hacking / testing feeds
-(void)processLoginWithData:(NSData*)data {
//
// Pull the data in from disk
//
NSString* responsePath = [[NSBundle mainBundle] pathForResource:@"response" ofType:@"xml"];
NSString* hackedString = [NSString stringWithContentsOfFile:responsePath
encoding:NSUTF8StringEncoding
error:nil];
data = [hackedString dataUsingEncoding:NSUTF8StringEncoding];
@rsaunders100
rsaunders100 / FileCommands.m
Created September 18, 2011 15:42
(iOS) Commands to save and load & copy from from the bundle and the documents directory.
+ (NSString*) pathForSaving
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"fileName.plist"];
}
+ (void) loadFromDisk {
NSString* path = [MyClass pathForSaving];