问题描述
我的工作与高速公路的WebSocket通信。
在我的应用程序的main.class,我设置为来电连接(),当用户点击一个按钮。
//切换按钮事件
TButton的=(切换按钮)findViewById(R.id.toggleButton1);
tButton.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
布尔器isChecked){ 如果(器isChecked){ }其他{ } }
});
和在这之后,有MyOffers.class,并且如果此类被访问时,
MyOffers_Fragment类会自动四次产生的,因为MyOffers.class包含旋转视图,有四种产品。
在'MyOffers_Fragment类,当用户点击产品的形象之一,应该发送消息。
如果(POS == 0){
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setOnClickListener(新ImageButton.OnClickListener(){
公共无效的onClick(视图v){
字符串ID =Product0;
Log.d(TAG,目前的产品是:+身份证);
A.sendMessage(ID);
}
});
}
但'mConnection.sendTextMessage(ID1);'此行令'NullPointerException异常的错误。
有一类Websocket_Connector.class
公共类WebSocket_Connector { 私有静态最后弦乐TAG =ECHOCLIENT;
公众最终WebSocketConnection mConnection =新WebSocketConnection(); 公共无效连接(最后弦乐wsuri){ Log.d(TAG,连接到:+ wsuri); 尝试{
mConnection.connect(wsuri,新WebSocketHandler(){ @覆盖
公共无效的OnOpen(){
Log.d(TAG,状态:连接到+ wsuri);
Log.d(TAG,连接成功\\ n!);
} @覆盖
公共无效onTextMessage(字符串有效载荷){
Log.d(TAG,回声得到:+负载);
} @覆盖
公共无效的OnClose(INT code,弦乐的原因){
Log.d(TAG,连接关闭。);
}
});
}赶上(WebSocketException E){ Log.d(TAG,e.toString());
} 公共无效的sendMessage(字符串消息){
连接(WS://192.168.x.xxx:XXXX);
mConnection.sendTextMessage(消息);
} }
我叫主网页类的连接(),之后尝试发送邮件。
但是,这不是你working..Can请帮我吗?
公共类WebSocket_Connector {私有静态最后弦乐TAG =ECHOCLIENT;
公众最终WebSocketConnection mConnection =新WebSocketConnection();
私人字符串tmpString =;
公共无效连接(最后弦乐wsuri){ Log.d(TAG,连接到:+ wsuri); 尝试{
mConnection.connect(wsuri,新WebSocketHandler(){ @覆盖
公共无效的OnOpen(){
Log.d(TAG,状态:连接到+ wsuri);
Log.d(TAG,连接成功\\ n!);
mConnection.sendTextMessage(tmpString);
tmpString =;
} @覆盖
公共无效onTextMessage(字符串有效载荷){
Log.d(TAG,回声得到:+负载);
} @覆盖
公共无效的OnClose(INT code,弦乐的原因){
Log.d(TAG,连接关闭。);
}
});
}赶上(WebSocketException E){ Log.d(TAG,e.toString());
} 公共无效的sendMessage(字符串消息){ 如果(mConnection.isConnected())
mConnection.sendTextMessage(消息);
其他{
tmpString =消息;
连接(WS://192.168.x.xxx:XXXX);
} } }
I am working on websocket communication with Autobahn.On the main.class of my app, I set to call 'connect()' when users click a button.
// Toggle Button event
tButton = (ToggleButton) findViewById(R.id.toggleButton1);
tButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
}else{
}
}
});
And after that, there is MyOffers.class, and if this class is accessed,MyOffers_Fragment class is produced four times automatically, because MyOffers.class contains 'carousel view' and there are four products.
On 'MyOffers_Fragment' class, when users click one of the image of products, message should be sent.
if (pos == 0) {
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setOnClickListener(new ImageButton.OnClickListener(){
public void onClick(View v){
String id = "Product0";
Log.d(TAG, "Current product is : " + id);
A.sendMessage(id);
}
});
}
But 'mConnection.sendTextMessage(id1);' this line makes 'NullPointerException' error.There is a class 'Websocket_Connector.class'
public class WebSocket_Connector {
private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
public void connect(final String wsuri) {
Log.d(TAG, "Connecting to: " + wsuri);
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri );
Log.d(TAG, "Connection successful!\n");
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection closed.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
public void sendMessage(String message) {
connect("ws://192.168.x.xxx:xxxx");
mConnection.sendTextMessage(message);
}
}
I called 'connect()' in the main page class, and after that try to send message.But, it's not working..Can you please help me out?
public class WebSocket_Connector {
private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
private String tmpString = "";
public void connect(final String wsuri) {
Log.d(TAG, "Connecting to: " + wsuri);
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri );
Log.d(TAG, "Connection successful!\n");
mConnection.sendTextMessage(tmpString);
tmpString = "";
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection closed.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
public void sendMessage(String message) {
if (mConnection.isConnected())
mConnection.sendTextMessage(message);
else {
tmpString = message;
connect("ws://192.168.x.xxx:xxxx");
}
}
}
这篇关于Android的 - 高速公路的WebSocket发送消息错误(NullPointerException异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!