Skip to content

Commit

Permalink
Android: updated User-Agent to Java-Android and tab spaces correction
Browse files Browse the repository at this point in the history
  • Loading branch information
raju authored and raju committed Jun 15, 2012
1 parent ae0590e commit 7b36df6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 89 deletions.
3 changes: 1 addition & 2 deletions android/PubnubAndroid/src/com/aimx/androidpubnub/Pubnub.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ private JSONArray _request(List<String> url_components) {
try { jsono.put("Failed JSONP HTTP Request."); } try { jsono.put("Failed JSONP HTTP Request."); }
catch (Exception jsone) {} catch (Exception jsone) {}


e.printStackTrace();
System.out.println(e); System.out.println(e);


return jsono; return jsono;
Expand Down Expand Up @@ -648,7 +647,7 @@ public String call() throws Exception {
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url); HttpUriRequest request = new HttpGet(url);
request.setHeader("V", "3.1"); request.setHeader("V", "3.1");
request.setHeader("User-Agent", "Android"); request.setHeader("User-Agent", "Java-Android");
request.setHeader("Accept-Encoding", "gzip"); request.setHeader("Accept-Encoding", "gzip");
httpclient.getParams().setParameter("http.connection.timeout", 310000); httpclient.getParams().setParameter("http.connection.timeout", 310000);


Expand Down
69 changes: 33 additions & 36 deletions android/PubnubAndroid/src/com/aimx/androidpubnub/PushService.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ private void generateNotification(Context context, String title,


// Launch new Intent to view a new contact added // Launch new Intent to view a new contact added
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, uri); Intent notificationIntent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent contentIntent = PendingIntent.getActivity( PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
getApplicationContext(), 0, notificationIntent, 0); notification.setLatestEventInfo(getApplicationContext(), title, message, contentIntent);
notification.setLatestEventInfo(getApplicationContext(), title,
message, contentIntent);


NotificationManager nm = (NotificationManager) context NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, notification); nm.notify(1, notification);


} }
Expand All @@ -82,9 +79,9 @@ public boolean execute(Object message) {
if(message instanceof JSONObject) if(message instanceof JSONObject)
{ {
JSONObject message1 =(JSONObject)message; JSONObject message1 =(JSONObject)message;
generateNotification(getApplicationContext(), generateNotification(getApplicationContext(),
message1.getString("title"), message1.getString("text"), message1.getString("title"), message1.getString("text"),
Uri.parse(message1.getString("url"))); Uri.parse(message1.getString("url")));
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Expand All @@ -99,53 +96,53 @@ class PushListener extends AsyncTask<String, Void, Boolean> {
protected Boolean doInBackground(String... params) { protected Boolean doInBackground(String... params) {
{ {
try { try {
// Callback Interface when a channel is connected // Callback Interface when a channel is connected
class ConnectCallback implements Callback { class ConnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when a channel is disconnected // Callback Interface when a channel is disconnected
class DisconnectCallback implements Callback { class DisconnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when a channel is reconnected // Callback Interface when a channel is reconnected
class ReconnectCallback implements Callback { class ReconnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when error occurs // Callback Interface when error occurs
class ErrorCallback implements Callback { class ErrorCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }

HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", params[0]); args.put("channel", params[0]);
args.put("callback", mPushReceiver); args.put("callback", mPushReceiver);
args.put("connect_cb", new ConnectCallback()); // callback to get connect event args.put("connect_cb", new ConnectCallback()); // callback to get connect event
args.put("disconnect_cb", new DisconnectCallback()); // callback to get disconnect event args.put("disconnect_cb", new DisconnectCallback()); // callback to get disconnect event (optional)
args.put("reconnect_cb", new ReconnectCallback()); // callback to get reconnect event args.put("reconnect_cb", new ReconnectCallback()); // callback to get reconnect event (optional)
args.put("error_cb", new ErrorCallback()); // callback to get error event args.put("error_cb", new ErrorCallback()); // callback to get error event (optional)
pubnub.subscribe(args); pubnub.subscribe(args);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion android/PubnubAndroidTest/src/com/fbt/Pubnub.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public String call() throws Exception {
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url); HttpUriRequest request = new HttpGet(url);
request.setHeader("V", "3.1"); request.setHeader("V", "3.1");
request.setHeader("User-Agent", "Android"); request.setHeader("User-Agent", "Java-Android");
request.setHeader("Accept-Encoding", "gzip"); request.setHeader("Accept-Encoding", "gzip");
httpclient.getParams().setParameter("http.connection.timeout", 310000); httpclient.getParams().setParameter("http.connection.timeout", 310000);


Expand Down
71 changes: 34 additions & 37 deletions android/PubnubAndroidTest/src/com/fbt/PubnubTestActivity.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public class PubnubTestActivity extends Activity {
/** Called when the activity is first created. */ /** Called when the activity is first created. */


Pubnub pubnub; Pubnub pubnub;
String myMessage=""; String myMessage = "", channel = "hello_world";
EditText ed; EditText ed;
RefreshHandler r = new RefreshHandler(); RefreshHandler r = new RefreshHandler();
String channel = "hello_world_rp3";
int limit = 1; int limit = 1;


@Override @Override
Expand All @@ -52,8 +51,8 @@ public void onClick(View v) {
// Create JSON Message // Create JSON Message
JSONObject message = new JSONObject(); JSONObject message = new JSONObject();
try { try {
if(ed.getText().toString() != null && !ed.getText().toString().equals("")) if(ed.getText().toString() != null && !ed.getText().toString().equals(""))
message.put( "Message", ed.getText().toString()); message.put( "Message", ed.getText().toString());
} }
catch (org.json.JSONException jsonError) {} catch (org.json.JSONException jsonError) {}


Expand All @@ -79,9 +78,9 @@ public void onClick(View v) {
Android: (Unsubscribe/disconnect) Android: (Unsubscribe/disconnect)
-------------------------------------------------------------------------------*/ -------------------------------------------------------------------------------*/


HashMap<String, Object> args = new HashMap<String, Object>(1); HashMap<String, Object> args = new HashMap<String, Object>(1);
args.put("channel", channel); args.put("channel", channel);
pubnub.unsubscribe( args ); pubnub.unsubscribe( args );
} }


}); });
Expand All @@ -105,15 +104,13 @@ public void onClick(View v) {
JSONArray response = pubnub.history( args ); JSONArray response = pubnub.history( args );


// Print Response from PubNub JSONP REST Service // Print Response from PubNub JSONP REST Service
//System.out.println(response);

try { try {
if(response != null){ if(response != null){
for (int i = 0; i < response.length(); i++) { for (int i = 0; i < response.length(); i++) {
JSONObject jsono = response.optJSONObject(i); JSONObject jsono = response.optJSONObject(i);
if(jsono != null){ if(jsono != null){
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Iterator keys = jsono.keys(); Iterator keys = jsono.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
System.out.print(jsono.get( keys.next().toString() ) +" "); System.out.print(jsono.get( keys.next().toString() ) +" ");
} }
Expand Down Expand Up @@ -201,6 +198,7 @@ public void AllMessageClick(View v)
response = pubnub.publish(args); response = pubnub.publish(args);
System.out.println(response); System.out.println(response);
} }

class RefreshHandler extends Handler { class RefreshHandler extends Handler {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
Expand Down Expand Up @@ -263,51 +261,51 @@ public boolean execute(Object message) {
// Callback Interface when a channel is connected // Callback Interface when a channel is connected
class ConnectCallback implements Callback { class ConnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when a channel is disconnected // Callback Interface when a channel is disconnected
class DisconnectCallback implements Callback { class DisconnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when a channel is reconnected // Callback Interface when a channel is reconnected
class ReconnectCallback implements Callback { class ReconnectCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Callback Interface when error occurs // Callback Interface when error occurs
class ErrorCallback implements Callback { class ErrorCallback implements Callback {


@Override @Override
public boolean execute(Object message) { public boolean execute(Object message) {
System.out.println(message.toString()); System.out.println(message.toString());
return false; return false;
} }
} }


// Listen for Messages (Subscribe) // Listen for Messages (Subscribe)
HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", channel); // Channel Name args.put("channel", channel); // Channel Name
args.put("callback", new Receiver()); // callback to get response args.put("callback", new Receiver()); // callback to get response
args.put("connect_cb", new ConnectCallback()); // callback to get connect event args.put("connect_cb", new ConnectCallback()); // callback to get connect event
args.put("disconnect_cb", new DisconnectCallback()); // callback to get disconnect event args.put("disconnect_cb", new DisconnectCallback()); // callback to get disconnect event (optional)
args.put("reconnect_cb", new ReconnectCallback()); // callback to get reconnect event args.put("reconnect_cb", new ReconnectCallback()); // callback to get reconnect event (optional)
args.put("error_cb", new ErrorCallback()); // callback to get error event args.put("error_cb", new ErrorCallback()); // callback to get error event (optional)
pubnub.subscribe( args ); pubnub.subscribe( args );


} catch (Exception e) { } catch (Exception e) {
Expand All @@ -325,7 +323,6 @@ protected void onPreExecute() {


protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
// result is the value returned from doInBackground // result is the value returned from doInBackground

} }
} }
} }
26 changes: 13 additions & 13 deletions android/README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ Caveats:


// Publish Message // Publish Message
// Create HashMap parameter // Create HashMap parameter
HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", "hello_world"); // Channel Name args.put("channel", "hello_world"); // Channel Name
args.put("message", message); // JSON Message args.put("message", message); // JSON Message


// Publish Message // Publish Message
JSONArray info = pubnub.publish( args ); JSONArray info = pubnub.publish( args );
Expand Down Expand Up @@ -115,8 +115,8 @@ Caveats:


// Create HashMap parameter // Create HashMap parameter
HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", "hello_world"); // Channel Name args.put("channel", "hello_world"); // Channel Name
args.put("callback", message_receiver); // Receiver Callback Class args.put("callback", message_receiver); // Receiver Callback Class


// Listen for Messages (Subscribe) // Listen for Messages (Subscribe)
pubnub.subscribe( args ); pubnub.subscribe( args );
Expand All @@ -127,9 +127,9 @@ Caveats:


```java ```java
// Create HashMap parameter // Create HashMap parameter
HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(1);
args.put("channel", "hello_world"); // Channel Name args.put("channel", "hello_world"); // Channel Name
// Get Unsubscribe // Get Unsubscribe
pubnub.unsubscribe( args ); pubnub.unsubscribe( args );


Expand All @@ -140,9 +140,9 @@ Caveats:
```java ```java
// Create HashMap parameter // Create HashMap parameter
HashMap<String, Object> args = new HashMap<String, Object>(2); HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", "hello_world"); // Channel Name args.put("channel", "hello_world"); // Channel Name
args.put("limit", 1); // Limit args.put("limit", 1); // Limit
// Get History // Get History
JSONArray response = pubnub.history( args ); JSONArray response = pubnub.history( args );


Expand All @@ -156,13 +156,13 @@ Caveats:


```java ```java
Pubnub pubnub = new Pubnub("demo", "demo"); Pubnub pubnub = new Pubnub("demo", "demo");
System.out.println(" Time: "+pubnub.time()); System.out.println(" Time: "+pubnub.time());


``` ```


## Java: (UUID) ## Java: (UUID)


```java ```java
System.out.println(" UUID: "+Pubnub.uuid()); System.out.println(" UUID: "+Pubnub.uuid());


``` ```

0 comments on commit 7b36df6

Please sign in to comment.