PubNub: Cannot Allow Method Subscription

I am new to Android programming and this is my first time using PubNub. I have included the .jar file in lib. I also imported it.

I am following the steps given here - http://www.pubnub.com/docs/android-java/pubnub-java-sdk#copy_and_paste_examples

but I am getting this error message. - "Unable to resolve method" subscribe (java.lang.String, anonymous javax.security.auth.callback.Callback) '

I am using Android Studio. Also, I am putting all my code in mainActivity. I'm not sure where exactly the code for pubnub goes.

My main activity is

 package com.example....<hidden>;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.parse.Parse;
import com.parse.ParseObject;
import com.pubnub.api.Pubnub;
import com.pubnub.api.PubnubError;
import com.pubnub.api.PubnubException;
import com.pubnub.api.*;
  import org.json.*;

  import javax.security.auth.callback.Callback;

  public class MainActivity extends ActionBarActivity {
private TextView testing;
Pubnub pubnub = new Pubnub("<mypubkey>", "<mysubkey>");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main)

    /* Subscribe to the demo_tutorial channel */
    try {
        pubnub.subscribe("demo_tutorial", new Callback() {
            public void successCallback(String channel, Object message) {
                System.out.println(message);
            }

            public void errorCallback(String channel, PubnubError error) {
                System.out.println(error.getErrorString());
            }
        });
    } catch (PubnubException e) {
        e.printStackTrace();
    }

}

      

My gradle is

 dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
compile fileTree(dir: 'libs', include: 'Pubnub-*.jar')
}

      

Please let me know how to use pubnub if I am doing it completely wrong. Those.

+3


source to share


1 answer


import javax.security.auth.callback.Callback;

      

There is your problem. Pubnub uses its own callback. The rest of your code looks good, but the reason it can't be solved is because there is no subscription implementation that takes values String

and javax.security.auth.callback.Callback

.

Remove that import from your code and everything should work as you included com.pubnub.api.*

. If you want to explicitly include it, the import statement looks like this:

import com.pubnub.api.Callback;

      

Another error that caused me a lot of problems when I started with PubNub on Android was forgetting to request the appropriate permissions online. Make sure the following lines appear in your manifest after the tag <manifest>

and before the tag <application>

:



<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

      

Also, I believe your gradle import should be fine if android studio recognizes the features. To make things easier in the future, feel free to include the PubNub library using the following dependency.

compile 'com.pubnub:pubnub:3.7.2' 

      

Good luck, let me know if you have any further questions!

+4


source







All Articles