android 什么版本支持websocket
展开全部
android使用websocket方法:
package com.fkapp.websocket;
import com.fkapp.websocket.R;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private final String TAG = "MainActivity";
public static String wsUrl = "ws://ip:port/chat"; /* TODO: 运行时替换ip port */
public WebSocketConnection wsC = new WebSocketConnection();
public Handler handler = new Handler()
{
@Override
public void handleMessage( Message msg )
{
super.handleMessage( msg );
if ( msg.what == 0 )
{
}
}
};
public void toastLog( String s )
{
Toast.makeText( this, s, Toast.LENGTH_SHORT ).show();
}
private void wsStart()
{
try {
wsC.connect( wsUrl, new WebSocketHandler()
{
@Override
public void onOpen()
{
toastLog( "Status: Connected to " + wsUrl );
wsC.sendTextMessage( "Hello, world!" );
}
@Override
public void onTextMessage( String payload )
{
toastLog( "Got echo: " + payload );
}
@Override
public void onClose( int code, String reason )
{
toastLog( "Connection lost." );
}
} );
} catch ( WebSocketException e ) {
e.printStackTrace();
}
}
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
wsStart();
Button wsSend = (Button) findViewById( R.id.wsSend );
wsSend.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick( View v )
{
wsC.sendTextMessage( "ooxx" );
}
} );
}
@Override
protected void onDestroy()
{
super.onDestroy();
if ( wsC.isConnected() )
{
wsC.disconnect();
}
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
/* Inflate the menu; this adds items to the action bar if it is present. */
getMenuInflater().inflate( R.menu.main, menu );
return(true);
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
/*
* Handle action bar item clicks here. The action bar will
* automatically handle clicks on the Home/Up button, so long
* as you specify a parent activity in AndroidManifest.xml.
*/
int id = item.getItemId();
if ( id == R.id.action_settings )
{
return(true);
}
return(super.onOptionsItemSelected( item ) );
}
}
package com.fkapp.websocket;
import com.fkapp.websocket.R;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private final String TAG = "MainActivity";
public static String wsUrl = "ws://ip:port/chat"; /* TODO: 运行时替换ip port */
public WebSocketConnection wsC = new WebSocketConnection();
public Handler handler = new Handler()
{
@Override
public void handleMessage( Message msg )
{
super.handleMessage( msg );
if ( msg.what == 0 )
{
}
}
};
public void toastLog( String s )
{
Toast.makeText( this, s, Toast.LENGTH_SHORT ).show();
}
private void wsStart()
{
try {
wsC.connect( wsUrl, new WebSocketHandler()
{
@Override
public void onOpen()
{
toastLog( "Status: Connected to " + wsUrl );
wsC.sendTextMessage( "Hello, world!" );
}
@Override
public void onTextMessage( String payload )
{
toastLog( "Got echo: " + payload );
}
@Override
public void onClose( int code, String reason )
{
toastLog( "Connection lost." );
}
} );
} catch ( WebSocketException e ) {
e.printStackTrace();
}
}
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
wsStart();
Button wsSend = (Button) findViewById( R.id.wsSend );
wsSend.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick( View v )
{
wsC.sendTextMessage( "ooxx" );
}
} );
}
@Override
protected void onDestroy()
{
super.onDestroy();
if ( wsC.isConnected() )
{
wsC.disconnect();
}
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
/* Inflate the menu; this adds items to the action bar if it is present. */
getMenuInflater().inflate( R.menu.main, menu );
return(true);
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
/*
* Handle action bar item clicks here. The action bar will
* automatically handle clicks on the Home/Up button, so long
* as you specify a parent activity in AndroidManifest.xml.
*/
int id = item.getItemId();
if ( id == R.id.action_settings )
{
return(true);
}
return(super.onOptionsItemSelected( item ) );
}
}
展开全部
他的意思可能是将web端的客户端的android版的客户端装在你现在的客户端上,你现在的客户端就有了web客户端了,但是android版的...汗!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
他的意思可能是将web端的客户端的android版的客户端装在你现在的客户端上,你现在的客户端就有了web客户端了,但是android版的...汗!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询