GithubHelp home page GithubHelp logo

micoinsmith / androidgaechannel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johnhaley81/androidgaechannel

0.0 2.0 0.0 156 KB

A wrapper that makes it easy to get Channel messages from a Google App Engine server on an Android device.

Java 100.00%

androidgaechannel's Introduction

This project is licensed under the Apache License, Version 2.0

About AndroidGAEChannel

This project simplifies the channel connection process between an Android device and a Google App Engine server.

A Channel connection from a GAE server requires a Javascript client so to get around this limitation in Android we load a WebView object in the background that loads a small HTML file with the necessary Javascript imbedded in it. This WebView object then allows commands to be sent to the Javascript engine loaded within the file and also allows the Javascript engine to send back messages, JSON objects, alerts and even call functions inside the Java class.

This functionality is leveraged to allow all channel communication sent from the server to be received and processed by the Android device.

Getting Started

After importing the ChannelAPI.java file and the com.axosoft.util package into your project you'll need 4 things:

  1. The path to your Google App Engine server's Javascript Channel API file. This is usually held at "_ah/channel/jsapi" (e.x. http://my-gae-server.appspot.com/_ah/channel/jsapi)

  2. The token that the server used to create the channel.

  3. An object that listens to all of the events that occur on the channel. The ChannelAPIEventListener needs to be implemented in order for the object to listen to the events that the ChannelAPI class fires.

  4. An Android Context that the class will be initialized under. This is used to properly initialize the WebView object

Here's a quick and dirty example of setting up the Channel:

package com.axosoft.example.ChannelAPI;

import android.content.Context;

import com.axosoft.util.ChannelAPI;
import com.axosoft.util.ChannelAPI.ChannelAPIEventListener;

public class ChannelAPIExample implements ChannelAPIEventListener {
  public static final String GAE_CHANNEL_API_JS_SOURCE = "http://my-gae-server.appspot.com/_ah/channel/jsapi";

  private String mToken;
  private ChannelAPI mChannel;

  public ChannelAPIExample(Context context, String token) {
    mToken = token;

    // Take the token and the context and start the ChannelAPI.
    mChannel = new ChannelAPI(context, GAE_CHANNEL_API_JS_SOURCE, token, this);
  }

  @Override
  public void onChannelAPIOpen() {
    // When the channel has been opened this method is called.
  }

  @Override
  public void onChannelAPIClosed() {
    // When the channel is closed this method will be called. 
    // NOTE: This method may not be called on all cases of the channel being closed (e.x. The server closes the channel
    //       and doesn't notify the client). The only way to be sure that the channel is actually open is to ping the
    //       the server and have a response sent back through the channel. Such functionality is outside the scope of
    //       this project and is not covered here.
  }

  @Override
  public void onChannelAPIUpdateEvent(String message) {
    // This is where the bulk of the code will go. Use this event to handle all messages sent back
    // from the server. JSON objects can be sent back as well and can be processed appropriately. 
  }

  @Override
  public void onChannelAPIError(String code, String description) {
    // Handle any error here.
  }

}

The above example shows how to initialize the channel and start recieving messages in 1 line. If you don't have the token or the ChannelAPIEventListener during the construction of the ChannelAPI event you can set those later and then call the "connect()" method to connect manually. If the "connect()" method is called before both the token and the listener is set it will not attempt a connect and will return false.

Problem?

If you encounter any issues with the code feel free to start an issue. I'll do my best to get back to you and help you out with any issues you may have.

Thanks!!

-John

androidgaechannel's People

Contributors

johnhaley81 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.