我一直在尝试创建一个Android服务,这将允许我维护websocket连接并及时发送一些数据.我在服务中创建一个处理程序来连接WebSocketClIEnt.但它没有连接.网址正在运行,我也测试了它.如果有人能提供帮助,我非常感谢.
主Activity.java
import androID.app.Activity;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.content.IntentFilter;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.TextVIEw;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); broadcastReceiver receiver=new broadcastReceiver() { @OverrIDe public voID onReceive(Context context, Intent intent) { TextVIEw text=(TextVIEw) findVIEwByID(R.ID.textVIEw); text.setText(intent.getStringExtra("msg")); } }; registerReceiver(receiver, new IntentFilter(SocketService.broADCAST_ACTION)); } // Method to start the service public voID startService(VIEw vIEw) { startService(new Intent(getBaseContext(), SocketService.class)); } // Method to stop the service public voID stopService(VIEw vIEw) { stopService(new Intent(getBaseContext(), SocketService.class)); }}
服务实施
import androID.app.Service;import androID.content.broadcastReceiver;import androID.content.Intent;import androID.os.Handler;import androID.os.IBinder;import androID.util.Log;import androID.Widget.Toast;import org.java_websocket.clIEnt.WebSocketClIEnt;import org.java_websocket.handshake.ServerHandshake;import java.net.URI;import java.util.logging.LogRecord;public class SocketService extends Service { boolean status=false; public static final String broADCAST_ACTION = "com.supun.broadcasttest"; broadcastReceiver broadcaster; broadcastReceiver broadrecIEver; Intent intent; Handler handler; WebSocketClIEnt clIEnt; @OverrIDe public IBinder onBind(Intent intent) { return null; } @OverrIDe public voID onCreate() { super.onCreate(); intent=new Intent(broADCAST_ACTION); handler=new Handler(); } @OverrIDe public int onStartCommand(final Intent intent, int flags, int startID) { Toast.makeText(this, "Service Started",Toast.LENGTH_LONG).show();// Let it continue running until it is stopped. //////////////////////////////////////////////////////////////////////////////// try { clIEnt = new WebSocketClIEnt(new URI( "ws://xxxxxxx-xxxx.rhcloud.com:8000/browser")) { @OverrIDe public voID onopen(ServerHandshake handshakedata) { Log.d("1", "open"); status = true; } @OverrIDe public voID onMessage(String message) { Log.d("reply", message); intent.putExtra("msg", message); sendbroadcast(intent); } @OverrIDe public voID one rror(Exception ex) { // Todo auto-generated method stub } @OverrIDe public voID onClose(int code, String reason, boolean remote{ status = false; } }; }catch(Exception e){ } ///////////////////////////////////////////////////////////////////////////////// handler.removeCallbacks(runnable); handler.postDelayed(runnable, 1000); return START_STICKY; } private Runnable runnable=new Runnable() { @OverrIDe public voID run() { clIEnt.connect(); for(int i=0; i<5;i++){ if(status){ intent.putExtra("msg","open"); sendbroadcast(intent); break; }else{ try { intent.putExtra("msg",clIEnt.getReadyState().toString()); sendbroadcast(intent); Thread.sleep(1000); } catch (InterruptedException e) { e.printstacktrace(); } } } if (!status) { intent.putExtra("msg","Time Out"); sendbroadcast(intent); } } }; @OverrIDe public voID onDestroy() { super.onDestroy(); Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); }}
我正在通过按钮单击事件调用Main Activity中的启动服务方法.输出总是快速转到Closing,最后显示为Timed out.我想知道可能是什么原因?
解决方法:
你使用的websocket客户端不是那种使用我用于项目的Engine IO客户端的强大尝试,性能卓越
https://github.com/nkzawa/engine.io-client.java
并非所有客户端都与您的服务器兼容,检查服务器上正在使用的内容,并且应该使用服务器和客户端上的不同库来复制该连接,从而导致断开连接
以上是内存溢出为你收集整理的java – 如何在android服务中建立WebSocket连接?全部内容,希望文章能够帮你解决java – 如何在android服务中建立WebSocket连接?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)