我正在用一些额外的信息重新制定问题,因为它处于“搁置”状态。我将删除另一个问题。
我正在尝试构建一个应用程序,它是一个控制台(如 cmd),我可以在其中发送订单(命令)并接收来自服务器的响应。
所以我应该在屏幕上看到的是我的订单,然后是服务器的响应。
为此我应该怎么做?
我正在尝试使用Service
. 但是服务不能与网络一起使用。
AsyncTask
当我发送第一个命令时将完成。所以每次我决定发送订单时都必须重新连接(效率低下?)
IntentService
绑定到Activity的生命。并以同样的方式完成Asynctask
。
我怎样才能做一个线程,它第一次连接到服务器。然后,每次我向线程发送消息时,它都会联系服务器并从服务器检索我的 TextEdit 信息?
针对 [on hold] 状态进行了编辑。添加更多信息
这是我所拥有的:
Activity
带有 aTextView
(用于终端提示)、aEditText
(允许用户输入他想要发送的命令)和 a来Button
发送它。这
Activity
是有界的Service
:ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { mBounded = false; mServer = null; } public void onServiceConnected(ComponentName name, IBinder service) { mBounded = true; StatusService.LocalBinder mLocalBinder = (StatusService.LocalBinder)service; mServer = mLocalBinder.getServerInstance(); /* once it is bounded, update data */ updateSensorsData(); } }; /* ---------------- LIFE CYCLE METHODS -------------------------*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_console); Intent mIntent = new Intent(this, StatusService.class); bindService(mIntent, mConnection, BIND_AUTO_CREATE); /* Scrollable TextView for Console Prompt*/ TextView tvConsole = (TextView) findViewById(R.id.tvConsole); tvConsole.setMovementMethod(new ScrollingMovementMethod()); }
应该与
Service
服务器交互并将信息检索到TextView
,所以我实现了:public String sendMessage(String message) { /* HERE I SHOULD CONNECT TO THE SERVER, BUT I DONT KNOW HOW */ String response; connect(server, port); sendReceiveCmdControl(message); return response; } protected String sendReceiveCmdControl(String msg) throws InterruptedException { String res; send(controlOut, msg+"\n"); // wait for response try { waitFor(controlIn, msg); } catch (EOFException e) { Log.d("ERROR", "Remote connection closed."); return "error"; } catch (IOException e) { Log.d("ERROR", "IO problem."); return "error"; } // end try // read the rest try { res = controlIn.readLine(); } catch (IOException e) { Log.d("ERROR", "IO problem."); return "error"; } // end try return(res); } // end sendReceiveCmdControl()
其中 controlOut 是 a PrintWriter
, controlIn a DataInputStream
,都在方法中初始化connect
如下:
/* Connection I/O */
public static Socket controlSocket = null;
public static PrintWriter controlOut = null;
public static DataInputStream controlIn = null;
public static Socket imageSocket = null;
public static PrintWriter imageOut = null;
public static DataInputStream imageIn = null;
protected void connect(String server, int port, boolean controlEnabled, boolean imageEnabled) {
if (imageEnabled) {
try {
imageSocket = new Socket(server, port);
imageOut = new PrintWriter(imageSocket.getOutputStream(), true);
imageIn = new DataInputStream(new BufferedInputStream(imageSocket.getInputStream()));
Log.d("INFO", "Connected to image socket correctly");
} catch (UnknownHostException e) {
disconnect();
Log.d("ERROR", "Exception: " + e.getMessage());
return;
} catch (IOException e) {
disconnect();
Log.d("ERROR", "Exception: " + e.getMessage());
return;
}
}
if (controlEnabled) {
try {
controlSocket = new Socket(server, port+1);
controlOut = new PrintWriter(controlSocket.getOutputStream(), true);
controlIn = new DataInputStream(new BufferedInputStream(controlSocket.getInputStream()));
Log.d("INFO", "Connected to control socket correctly");
} catch (UnknownHostException e) {
disconnect();
Log.d("ERROR", "Exception: " + e.getMessage());
return;
} catch (IOException e) {
disconnect();
Log.d("ERROR", "Exception: " + e.getMessage());
return;
}
}
}
所以我想做的是:
connect
第一次运行服务时使用方法连接到服务器。- 每次我想向远程服务器发送订单时,我都应该
mServer.sendMessage(command);
在我的活动中进行。 - 因此
Service
与 Remote 交互Server
,当它获得响应时,Service
检索对Activity
UI 更新的响应。
如果有比Activity-Service-Thread/Whatever...
让我知道的其他正确方法,请感谢您的帮助。感谢您的耐心等待。