Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix-1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Klein committed Aug 20, 2013
2 parents 95eed4c + 8f53726 commit e1ee956
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 154 deletions.
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ devices.

## Installation

[Download] [3] the latest release as a `*.jar` file and add it to your Android app's `libs/` folder. Android will
take care of adding the dependency to your classpath. You're already good to go now. If you want more from life than
simplicity: read [this] [4].
Download the [latest release] [2] as a `*.jar` file and add it to your app's `libs/` folder. Android will take care of
adding the dependency to your classpath. You're already good to go now. If you want more from life than simplicity: read
[this] [3].

## Usage

Expand All @@ -26,14 +26,13 @@ trouble building it then there's an `*.apk` in the download section waiting for

````java
import static com.taig.communicator.method.Method.*;
import com.taig.communicator.result.Text;
import com.taig.communicator.result.Image;
import com.taig.communicator.result.Parser;

Response.Payload<String> source = GET( Text.class, "http://www.android.com/" ).followRedirects( true ).request();
Response.Payload<Bitmap> logo = GET( Image.class, "http://www.android.com/images/logo.png" ).request();
Response.Payload<String> source = GET( Parser.TEXT, new URL( "http://www.android.com/" ) ).followRedirects( true ).request();
Response.Payload<Bitmap> logo = GET( Parser.IMAGE, new URL( "http://www.android.com/images/logo.png" ) ).request();
````

The first argument, `Text.class`, is a Parser that will be executed in order to process the connection's `InputStream`.
The first argument, `Parser.TEXT`, is a Parser that will be executed in order to process the connection's `InputStream`.
After successful request execution you will retrieve a `Response<T>` object that wraps the parsed payload as well as
the source's URL and the server's response headers.

Expand Down Expand Up @@ -65,7 +64,7 @@ Events provide a very simple way of interacting with your application during req
`com.taig.communicator.Method` takes an additional parameter `Event<T>`.

````java
GET<String>( Text.class, "http://www.android.com/", new Event<String>()
GET<String>( Parser.TEXT, new URL( "http://www.android.com/" ), new Event<String>()
{
@Override
protected void onReceive( int progress )
Expand Down Expand Up @@ -97,7 +96,7 @@ Parameter params = new Parameter();
params.put( "email", "my.taig@gmail.com" );
params.put( "pass", "As if!" );

POST( Text.class, "https://facebook.com/login.php", params ).run();
POST( Parser.TEXT, new URL( "https://facebook.com/login.php" ), params ).run();
````

The supplied data will then be properly encoded for transmission and the request headers `Content-Length` and
Expand All @@ -114,7 +113,7 @@ Data data = new Data.Multipart.Builder()
.addTextFile( "cv", new File( "/my_cv.txt" ), "utf-8" )
.build();

POST( Text.class, "http://some.webservice.com", data ).run();
POST( Parser.TEXT, new URL( "http://some.webservice.com" ), data ).run();
````

#### Cookies
Expand All @@ -123,9 +122,9 @@ Last but not least you can also add cookies to a request header. The common user
`Set-Cookie` directive in a server's response header.

````java
Response<Void> response = HEAD( "https://www.google.com" ).request();
Response<Void> response = HEAD( new URL( "https://www.google.com" ) ).request();

GET( Text.class, "https://www.google.com" )
GET( Parser.TEXT, new URL( "https://www.google.com" ) )
.addCookie( new HttpCookie( "remember_me", "true" )
.addHeader(
COOKIE,
Expand All @@ -139,14 +138,14 @@ Furthermore *Communicator* comes with a `CookieStore` implementation that persis

````java
CookieStore store = new PersistedCookieStore( MyActivity.this );
Response response = HEAD( "https://www.google.com" ).request();
Response response = HEAD( new URL( "https://www.google.com" ) ).request();

for( HttpCookie cookie : response.getCookies() ) // Persist retrieved cookies.
{
store.add( reponse.getURL().toURI(), cookie );
}

GET( Text.class, "https://www.google.com" ) // Send persisted cookies that are associated with
GET( Parser.TEXT, new URL( "https://www.google.com" ) ) // Send persisted cookies that are associated with
.putCookie( store ) // "google.com" along with the request.
.run();
````
Expand All @@ -160,9 +159,9 @@ connections. It will then go ahead and spawn the same amount of Threads in order
````java
Communicator communicator = new Communicator( 2 );
communicator.execute( GET<String>( Text.class, "http://www.example.org" ) );
communicator.execute( GET<String>( Text.class, "http://www.example.com" ) );
communicator.execute( GET<String>( Text.class, "http://www.example.net" ) );
communicator.execute( GET<String>( Text.class, new URL( "http://www.example.org" ) ) );
communicator.execute( GET<String>( Text.class, new URL( "http://www.example.com" ) ) );
communicator.execute( GET<String>( Text.class, new URL( "http://www.example.net" ) ) );
````
> **Please Note**
Expand All @@ -173,7 +172,7 @@ communicator.execute( GET<String>( Text.class, "http://www.example.net" ) );
to declare it's priority via the `execute( Runnable runnable, boolean skipQueue )` method.

````java
communicator.execute( GET<String>( Text.class, "http://www.example.xxx" ), true );
communicator.execute( GET<String>( Parser.TEXT, new URL( "http://www.example.xxx" ) ), true );
````

By default `Communicator` drops all cookies as its policy says `CookiePolicy.ACCEPT_NONE`. You can changes this behavior
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Build extends sbt.Build
val main = Project( "communicator", file( "." ) ).settings(
name := "Communicator",
organization := "com.taig.communicator",
version := "1.0",
version := "1.0.1",
autoScalaLibrary := false,
libraryDependencies += "com.google.android" % "android" % "4.3" % "provided" from ( "file://" + System.getenv( "ANDROID_HOME" ) + "/platforms/android-18/android.jar" )
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected void onCreate( Bundle savedInstanceState )
final Map<Integer, Class<? extends Interaction>> activities = new HashMap<Integer, Class<? extends Interaction>>();
activities.put( button_simple_request, SimpleRequest.class );
activities.put( button_events, Events.class );
activities.put( button_image, Image.class );
activities.put( button_custom_result_parser, CustomResultParser.class );
activities.put( button_form_send, FormSend.class );
activities.put( button_cookies, Cookies.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.view.Gravity;
import com.taig.communicator.data.PersistedCookieStore;
import com.taig.communicator.request.Response;
import com.taig.communicator.result.Text;
import com.taig.communicator.result.Parser;

import java.net.CookieStore;
import java.net.HttpCookie;
Expand Down Expand Up @@ -39,13 +39,13 @@ public void interact() throws Exception
HttpCookie localCookie = new HttpCookie( "local", "cookie" );
localCookie.setVersion( 0 );

final String manual = GET( Text.class, new URL( "http://httpbin.org/get" ) )
final String manual = GET( Parser.TEXT, new URL( "http://httpbin.org/get" ) )
.putCookie( response )
.addCookie( localCookie )
.request()
.getPayload();

final String automatic = GET( Text.class, new URL( "http://httpbin.org/get" ) )
final String automatic = GET( Parser.TEXT, new URL( "http://httpbin.org/get" ) )
.putCookie( store )
.request()
.getPayload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CustomResultParser( Context context )
@Override
public void interact() throws Exception
{
GET( Headline.class, new URL( "http://stackoverflow.com" ), new Event.Payload<String>()
GET( new Headline(), new URL( "http://stackoverflow.com" ), new Event.Payload<String>()
{
@Override
protected void onSuccess( String content )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Events extends Interaction

public Events( Context context )
{
super( context, View.inflate( context, R.layout.progress, null ) );
super( context, R.layout.progress );
this.progressBar = (ProgressBar) main.findViewById( R.id.progress_bar_events );
this.cancel = (Button) main.findViewById( R.id.button_cancel );
}
Expand All @@ -37,7 +37,7 @@ public String getIdleText()
@Override
public void interact() throws Exception
{
final Get request = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
final Get request = GET( new ReadAndIgnore(), new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
{
@Override
protected void onEvent( State state )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.content.Context;
import android.view.Gravity;
import com.taig.communicator.event.Event;
import com.taig.communicator.data.Data;
import com.taig.communicator.data.Parameter;
import com.taig.communicator.result.Text;
import com.taig.communicator.event.Event;
import com.taig.communicator.result.Parser;

import java.net.URL;

Expand All @@ -26,7 +26,7 @@ public void interact() throws Exception
params.put( "password", "strawberry" );
params.put( "remember", "true" );

POST( Text.class, new URL( "http://httpbin.org/post" ), new Data.Form( params, "utf-8" ), new Event.Payload<String>()
POST( Parser.TEXT, new URL( "http://httpbin.org/post" ), new Data.Form( params, "utf-8" ), new Event.Payload<String>()
{
@Override
protected void onSuccess( String content )
Expand Down
43 changes: 43 additions & 0 deletions sample/src/main/java/com/taig/communicator/sample/io/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.taig.communicator.sample.io;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.taig.communicator.event.Event;
import com.taig.communicator.result.Parser;
import com.taig.communicator.sample.R;

import java.net.URL;

import static com.taig.communicator.method.Method.GET;

public class Image extends Interaction
{
protected ImageView image;

public Image( Context context )
{
super( context, R.layout.image );
this.image = (ImageView) main.findViewById( R.id.image );
}

@Override
public void interact() throws Exception
{
GET( Parser.IMAGE, new URL( "http://minionslovebananas.com/images/check-in-minion.jpg" ), new Event.Payload<Bitmap>()
{
@Override
protected void onSuccess( Bitmap bitmap )
{
getTextView().setText( "(-:" );
image.setImageBitmap( bitmap );
}

@Override
protected void onFailure( Throwable error )
{
getTextView().setText( error.getMessage() );
}
} ).run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public abstract class Interaction

public Interaction( Context context )
{
this( context, View.inflate( context, R.layout.text, null ) );
this( context, R.layout.text );
}

public Interaction( Context context, int layout )
{
this( context, View.inflate( context, layout, null ) );
}

public Interaction( Context context, View main )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.taig.communicator.event.Event;
import com.taig.communicator.method.Get;
import com.taig.communicator.request.Response;
import com.taig.communicator.result.Parser;
import com.taig.communicator.sample.R;
import com.taig.communicator.sample.result.ReadAndIgnore;

Expand All @@ -34,7 +35,7 @@ public class MultipleConnections extends Interaction

public MultipleConnections( Context context )
{
super( context, View.inflate( context, R.layout.multiple_progress, null ) );
super( context, R.layout.multiple_progress );
this.progressBar1 = (ProgressBar) main.findViewById( R.id.progress_bar_multiple_connections_1 );
this.progressBar2 = (ProgressBar) main.findViewById( R.id.progress_bar_multiple_connections_2 );
this.progressBar3 = (ProgressBar) main.findViewById( R.id.progress_bar_multiple_connections_3 );
Expand All @@ -54,8 +55,9 @@ public String getIdleText()
public void interact() throws Exception
{
final Communicator communicator = new Communicator( 2 );
Parser<Void> ignore = new ReadAndIgnore();

Get one = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
Get one = GET( ignore, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
{
@Override
protected void onReceive( int progress )
Expand All @@ -70,7 +72,7 @@ protected void onSuccess( Response.Payload<Void> response )
}
} );

Get two = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin.gz" ), new Event.Payload<Void>()
Get two = GET( ignore, new URL( "http://vhost2.hansenet.de/1_mb_file.bin.gz" ), new Event.Payload<Void>()
{
@Override
protected void onReceive( int progress )
Expand All @@ -85,7 +87,7 @@ protected void onSuccess( Response.Payload<Void> response )
}
} );

Get three = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
Get three = GET( ignore, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
{
@Override
protected void onReceive( int progress )
Expand All @@ -100,7 +102,7 @@ protected void onSuccess( Response.Payload<Void> response )
}
} );

Get four = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
Get four = GET( ignore, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
{
@Override
protected void onReceive( int progress )
Expand All @@ -115,7 +117,7 @@ protected void onSuccess( Response.Payload<Void> response )
}
} );

Get five = GET( ReadAndIgnore.class, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
Get five = GET( ignore, new URL( "http://vhost2.hansenet.de/1_mb_file.bin" ), new Event.Payload<Void>()
{
@Override
protected void onReceive( int progress )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import com.taig.communicator.event.Event;
import com.taig.communicator.request.Response;
import com.taig.communicator.result.Text;
import com.taig.communicator.result.Parser;

import java.net.URL;

Expand All @@ -19,7 +19,7 @@ public SimpleRequest( Context context )
@Override
public void interact() throws Exception
{
GET( Text.class, new URL( "http://www.gutenberg.org/files/43206/43206-0.txt" ), new Event.Payload<String>()
GET( Parser.TEXT, new URL( "http://www.gutenberg.org/files/43206/43206-0.txt" ), new Event.Payload<String>()
{
@Override
protected void onSuccess( Response.Payload<String> response )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.content.Context;
import android.text.Html;
import com.taig.communicator.event.Event;
import com.taig.communicator.data.Data;
import com.taig.communicator.data.Parameter;
import com.taig.communicator.result.Text;
import com.taig.communicator.event.Event;
import com.taig.communicator.result.Parser;

import java.net.URL;

Expand All @@ -26,7 +26,7 @@ public void interact() throws Exception
.addParameter( new Parameter( "note", "This is a test upload from Communicator/Android" ) )
.build();

POST( Text.class, new URL( "http://cgi-lib.berkeley.edu/ex/fup.cgi" ), data, new Event.Payload<String>()
POST( Parser.TEXT, new URL( "http://cgi-lib.berkeley.edu/ex/fup.cgi" ), data, new Event.Payload<String>()
{
@Override
protected void onSuccess( String payload )
Expand Down
19 changes: 19 additions & 0 deletions sample/src/main/res/layout/image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:padding="10dp" />

<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:contentDescription="@string/image" />

</LinearLayout>
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
android:padding="10dp"
android:text="@string/events" />

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button_image"
android:padding="10dp"
android:text="@string/image" />

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button_custom_result_parser"
Expand Down
Loading

0 comments on commit e1ee956

Please sign in to comment.