【发布时间】:2013-06-14 14:51:14
【问题描述】:
按照你的推荐,我上了三等功!!
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());
}
}
}
在另一个类中,我正在尝试通过字符串类型“id”访问连接
public class Myoffers_Fragment extends Fragment {
private static final String TAG = "ECHOCLIENT";
public String id;
public static Fragment newInstance(Myoffers context, int pos,
float scale)
{
Bundle b = new Bundle();
b.putInt("pos", pos);
b.putFloat("scale", scale);
return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
WebSocket_Connector A = new WebSocket_Connector();
LinearLayout l = (LinearLayout)
inflater.inflate(R.layout.mf, container, false);
int pos = this.getArguments().getInt("pos");
TextView tv = (TextView) l.findViewById(R.id.text);
tv.setText("Position = " + pos);
ImageView product_photo = (ImageView) l.findViewById(R.id.myoffer_image);
switch(pos){
case 0:
product_photo.setImageResource(R.drawable.myoffers_0);
Log.d(TAG, "Current pos" + pos);
Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
id = "product 0";
A.mConnection.sendTextMessage(id);
break;
你能看到“A.mConnection.sendTextMessage(id);”吗?? 这是正确的做法吗?
【问题讨论】:
-
为什么这个问题没有显示任何努力???我寻找其他问题,但那些无法解决我的问题!
-
首先,
mConnection没有在哪里声明。其次,您没有显示sendTextMessage(id)的代码,就我们所知,这可能是一种不存在的方法。第三,id声明在哪里?澄清你的问题,你可能会得到帮助。从对答案的评论中,这不应该也被标记为 Android 吗? -
声明了mConnection和sendTextMessage(id)和id。
-
您遇到的错误是什么?编译时间,在你的 IDE 中?目前还不清楚需要什么样的帮助。据我所知,你永远不会在
A上调用connect,这可能是sendTextMessage不起作用的原因。 -
@JonathanDrapeau 在连接器类中,它生成包含在库 jar 文件中的“WebSocketConnection”类。我想通过另一个类的“连接器”类访问“WebSocketConnection”。我想问我如何从另一个类访问“WebSocketConnection”?清楚了吗?