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.
Related
I am testing platform specific coding with Flutter. I opened the android folder in Android Studio, wrote my code and it shows no errors. I then moved back to Flutter which is underlining in red the Rs in the code below. However, the app is working fine. I am wondering why an error is showing and what I can do to remove these red lines. I would be grateful for pointers.
Here is my code:
public class MyTileService extends TileService {
private final int STATE_ON = 1;
private final int STATE_OFF = 0;
private int toggleState = STATE_ON;
#Override
public void onTileAdded() {
System.out.println("onTileAdded");
}
#Override
public void onTileRemoved() {
System.out.println("onTileRemoved");
}
#Override
public void onStartListening() {
System.out.println("onStartListening");
}
#Override
public void onStopListening() {
System.out.println("onStopListening");
}
#Override
public void onClick() {
System.out.println("onClick state = " + Integer.toString(getQsTile().getState()));
Icon icon;
if (toggleState == STATE_ON) {
toggleState = STATE_OFF;
icon = Icon.createWithResource(getApplicationContext(), R.drawable.ic_spa_black_24dp);
System.out.println("OFF");
} else {
toggleState = STATE_ON;
icon = Icon.createWithResource(getApplicationContext(), R.drawable.ic_star_border_black_24dp);
System.out.println("ON");
}
getQsTile().setIcon(icon);
getQsTile().updateTile();
}
}
I tried cleaning the android project, uninstalling and reinstalling Flutter and Dart plugins, checking flutter doctor.
Here is an image:
Flutter projects are not Gradle-based Android projects so a number of
features provided by Android Studio are not available to Flutter
programmers.
This still is a Flutter Plugin.
Is documented here:
https://github.com/flutter/flutter-intellij/issues/2262
And you can track it here:
https://github.com/flutter/flutter-intellij/issues/2243
It seems that the first item is check, so it's only a metter of time to wait this milestone to be released.
Cordova can't build the project with this plugin.
When I import the project in eclipse, eclipse shows error in ParsePlugin.java
private void initialize(final CallbackContext callbackContext, final JSONArray args) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
PushService.setDefaultPushCallback(cordova.getActivity(), cordova.getActivity().getClass());
ParseInstallation.getCurrentInstallation().saveInBackground();
callbackContext.success();
}
});
}
This issue fixed by bolts-android.XXX.jar from Parse.zip
and copy it to platform/android/lib/
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
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.
I cloned latest version of volley from https://android.googlesource.com/platform/frameworks/volley
I imported it in Eclipse, and tried to run the test project, but I get ClassNotFoundException
java.lang.NoClassDefFoundError: com.android.volley.mock.WaitableQueue$MagicStopRequest
at com.android.volley.mock.WaitableQueue.<init>(WaitableQueue.java:31)
at com.android.volley.CacheDispatcherTest.setUp(CacheDispatcherTest.java:45)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619)
How do I run theses tests?
Thank you.
The Volley framework repo comes with support for both Ant and Gradle based build systems. Running the tests using the Gradle build system is quite simple. I did briefly try using the Ant approach, but it wasn't as straight forward and I prefer Gradle anyhow.
First thing you'll need is to install Gradle 1.10, which is the version required by Volley as of writing this answer. Alternatively, you could clone this mirror repo, which includes some extras, such as gradlew support (Gradle bootstrapping utility for downloading and using the proper version of Gradle).
Then simply execute the following command from the project root (using either gradle or gradlew based on which option you chose above):
$> gradle clean connectedCheck
This will run the full gambit of tasks, including the tests. It should produce an HTML test report at build/reports/instrumentedTests/connected/index.html, relative to the project root.
I found this to get start with.
I write this Test Project , and import com.android.volley and com.android.volley.toolbox from the volley library. And it works ^_^
public class MainActivity extends Activity {
protected static final String TAG = "com.gyh.myvolleytest";
private static Response.ErrorListener createErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error Response code: " + error.getMessage());
}
};
}
private static Response.Listener<String> createSuccessListener() {
return new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// TODO parse response
String string = response.toString();
Log.d(TAG, "string :"+string);
}
};
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//it is the only button in the layout ,click to log the result
public void click(View view) {
RequestQueue queue = Volley.newRequestQueue(this);
String url="http://192.168.1.108:8080/httptest/servlet/mainservlet?name=stack&age=23";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
createSuccessListener(),
createErrorListener());
queue.add(request);
queue.start();
}
}
let me know what happens ^_^