I've got a Andriod Studio project and I'm trying to use the SocketIOClient from the AndroidAsync library. I'm building the library with the command:
dependencies {
compile 'com.koushikdutta.androidasync:AndroidAsync:1.0.0'
}
I was able to get the websocket example to work, but there's seems to be a version issue with the 1.0 and the SocketIOClient example. I've also tried using the 2.0 jar file, but SocketIOClient doesn't appear to be defined there at all(this is one of my first andriod applications, so maybe I'm just doing something wrong). Does anyone know what needs to be done to get the following code to compile:
SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://192.168.1.2:3000", new ConnectCallback() {
#Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {
if (ex != null) {
ex.printStackTrace();
return;
}
client.setStringCallback(new StringCallback() {
#Override
public void onString(String string) {
System.out.println(string);
}
});
client.on("someEvent", new EventCallback() {
#Override
public void onEvent(JSONArray argument, Acknowledge acknowledge) {
System.out.println("args: " + arguments.toString());
}
});
client.setJSONCallback(new JSONCallback() {
#Override
public void onJSON(JSONObject json) {
System.out.println("json: " + json.toString());
}
});
}
});
Thanks,
Scott
Related
i have implemented quickblox successfully earlier for my android chat apps for only text chat. this time i want to send message with image as attachment. File is sending successfully ( i have checked it in quickblox admin Content panel). But in the receiver part it is not displaying image or text. I am getting nor any Exception/ error or text in logcat. So it's very difficult to trace out the wrong code, i m doing. plz help.
MyChatControlle
------------------------`private QBPrivateChatManagerListener chatManagerListener=new QBPrivateChatManagerListener() {
#Override
public void chatCreated(QBPrivateChat qbPrivateChat, boolean b) {
if(!b) {
qbPrivateChat.addMessageListener(myListener);
}
}
};`
private QBMessageListener myListener=new QBMessageListener() {
#Override
public void processMessage(QBChat qbChat, QBChatMessage qbChatMessage) {
int frm=qbChatMessage.getSenderId();
int tos=qbChatMessage.getRecipientId();
System.out.println(String.format(">>> Message received (from=%s, to=%s): %s", frm, tos, qbChatMessage.getBody()));
if (onMessageReceivedListener != null) {
onMessageReceivedListener.onMessageReceived(qbChatMessage);
}
}
#Override
public void processError(QBChat qbChat, QBChatException e, QBChatMessage qbChatMessage) { }
};
ChatActivity
MyChatController.OnMessageReceivedListener onMessageReceivedListener = new MyChatController.OnMessageReceivedListener() {
#Override
public void onMessageReceived(final QBChatMessage msg) {
try {
final String mmsg = msg.getBody();
Toast.makeText(getApplicationContext(), "Receiving..."+mmsg, Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(getApplicationContext(),"#A#B#"+e.toString(), Toast.LENGTH_LONG).show();
}
}
};
You can use latest version of QuickBlox Android SDK without chat managers. In newest versions you can use QBChatDialog model to perform all chat functions. Additionally check all parameters of your attachment, they mast be not null and not empty. Can you provide message, which didn't catch by listener (.xml from logcat)?
I am trying to implement a referral code system, and I am using Branch.io Metrics library. The problem I am facing is that the documentation is not good (doesn't work) and I am unable to generate a code
Documentation:
https://github.com/BranchMetrics/Branch-Android-SDK#register-an-activity-for-direct-deep-linking-optional-but-recommended
Here are the steps I have taken including adding the library.
1) Grabbed the jar, added to my libs folder and added the following to my depenencies
compile files('libs/branch-1.5.9.jar')
2) In my application class that extends Application I added the following
if (DEBUG) {
Branch.getAutoTestInstance(this);
} else {
Branch.getAutoInstance(this);
}
3) In my AndroidManifest.xml I added the following
<meta-data android:name="io.branch.sdk.BranchKey" android:value="#string/bnc_app_key" />
4) In the Activity that I am testing everything, I added in the onStart() method the following
#Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
#Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
From the above, I believe I have successfully created a branch.io session and a listener that will allow me to retrieve data if branchError is null (there are no conflicts)
While still inside onStart() I now try to generate a referral code. So the whole onStart() looks as follows:
#Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
#Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
branch.getReferralCode(5, new BranchReferralInitListener() {
#Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
try {
String code = jsonObject.getString("referral_code");
Log.d(TAG, "code: " + code);
} catch (JSONException e) {
Log.e(TAG, "JSONException :: " + e);
e.printStackTrace();
}
}
});
}
5) I added onNewIntent override method
#Override
protected void onNewIntent(Intent intent) {
this.setIntent(intent);
}
My app does not reach inside of the onInitFinished listener, so I am unable to retrieve any code(s). Any suggestions on what I have missed is appreciated, and hopefully this thread will fill the holes that the documentation lacks.
I'm going to assume that the following lines of code are properly closed and aren't throwing syntax errors:
branch.initSession(new BranchReferralInitListener() {
#Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this); // parenthesis wasnt here
First, if you are extending the Application class, you need to initialize branch inside the onCreate() callback of your Application class:
public void onCreate() {
super.onCreate();
Branch.getInstance(this);
}
As per the Branch sample code found here, you actually need to call:
Branch.getInstance();
Instead of:
Branch.getInstance(getApplicationContext());
Inside your activities onCreate callback. Once that is taken care of, the Branch SDK will properly create.
Working with connectSDK Android for casting videos on samsung android Tv.
I've successfully launch the default youtube app and played as well.
But when ever i'm trying to stop the currently running Youtube video getting a response code = 400 i.e
com.connectsdk.service.command.ServiceCommandError: Bad Request
Anyone worked on this? or familiar with the issue?
Code:
if (runningAppSession != null) {
if (runningAppSession.getSessionType() == LaunchSessionType.App) {
getLauncher().closeApp(runningAppSession, new ResponseListener<Object>() {
#Override
public void onError(ServiceCommandError error) {
System.out.println(error);
}
#Override
public void onSuccess(Object object) {
System.out.println(object.toString());
}
});
}
}
This question is related to How to build a Maven Android project in eclipse. Using suggestions from #user714965 I was able to build the code in project : https://github.com/nkzawa/socket.io-client.java. Now I am trying to compile an Android test project. I created a new Android project and under Java Build Path specified the socket.io-client project as dependency. But I am getting compilation errors. Eclipse is unable to find Emitter Class.
This is the relevant code:
public class SocketTask extends AsyncTask<Void, Void, Void> {
Socket mSocket = null;
#Override
protected Void doInBackground(Void... arg0) {
try {
Log.d(TAG, "Connecting to server");
mSocket = IO.socket("<my tested socket.io server address>");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
#Override
public void call(Object... args) {
Log.d(TAG, "Connected");
}
}).on("event", new Emitter.Listener() {
#Override
public void call(Object... args) {
Log.d(TAG, "Event");
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
#Override
public void call(Object... args) {
Log.d(TAG, "Disconnect");
}
});
mSocket.connect();
return null;
}
}
It looks like dependent libraries are not resolved. Did you install all of them correctly?
engine.io-client 0.3.0
Java-WebSocket 1.3.0
org.json 20090211 (installed in Android as default)
Emitter is included in engine.io-client.
I cannot respond to the comments (new StackOverflow user) but I have gotten the library to work in Android Studio via build.gradle. If you are primarly developing for Android they suggest moving to Android Studio as it will soon be the supported IDE.
EDIT
I can connect to a socketIO server built on nodejs from an android application with this library from nkzawa. Reference to repository.
Add library as gradle dependency:
compile 'com.github.nkzawa:socket.io-client:0.3.0'
I wrote a class to hold my socket:
public class SocketIO{
Socket sock = null;
public SocketIO(){
try{
sock = IO.socket('<connection string>');
//Place all events here as per documention
sock.on(Socket.EVENT_CONNECT, new Emitter.Listener(){
#Override
public void call(Object... args){
System.out.println("connect");
}
});
sock.connect();
} catch(URISyntaxException e){
e.printStatckTrace();
}
};
//Helper function for emiting from android
public void sEmit(String event, JSONObject obj){
if(socket.connected()){
sock.emit(event, obj);
}
}
}
And use it in my activity:
SocketIO socket = new SocketIO();
I had some initial trouble getting the library to work but it has been flawless since, the "issues" section of the github repository is very helpful.
Hope that helps.
Just starting in Android dev, I would like to use Android Websockets code in my new project in Android Studio.
OS X 10.8.x, AS 0.4.0
I followed: How to import eclipse library project from github to android studio project?
Everything worked great until I tried to actually code. I pasted in (from the example):
List<BasicNameValuePair> extraHeaders = Arrays.asList(
new BasicNameValuePair("Cookie", "session=abcd")
);
WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Listener() {
#Override
public void onConnect() {
Log.d(TAG, "Connected!");
}
#Override
public void onMessage(String message) {
Log.d(TAG, String.format("Got string message! %s", message));
}
#Override
public void onMessage(byte[] data) {
Log.d(TAG, String.format("Got binary message! %s", toHexString(data)));
}
#Override
public void onDisconnect(int code, String reason) {
Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
}
#Override
public void onError(Exception error) {
Log.e(TAG, "Error!", error);
}
}, extraHeaders);
client.connect();
// Later…
client.send("hello!");
client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF });
client.disconnect();
When I build/run the project, I get:
error: cannot find symbol class WebSocketClient
I think my question is: What is the import statement I should use?
How would I go about adding/using this code?
You ran into a bug that was fixed in Android Studio 0.4.3. I recommend you upgrade.
The auto importer was broken. Once I restarted Android Studio it showed I should put:
import com.codebutler.android_websockets.WebSocketClient;
As my import. Now I have a NullPointerException for a particular piece of code, but that's a different problem.