[英]Java server that waits for message from client
我正在嘗試將一個字符串從 Android 應用程序(客戶端)發送到一個 Java 服務器,該服務器等待來自許多 android 客戶端的此類消息並將它們存儲在一個文件中。 客戶端編碼很好,我在教程和博客的幫助下寫的,這里是:
HttpURLConnection conn = null;
try {
Log.e("URL", "> " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
我想要的是一個 Java 服務器,它可以處理來自不同 android 應用程序的這些消息,並將消息字符串存儲到文本文件中。 我只想知道如何處理這些傳入的消息並將它們放入一個空字符串中。
到目前為止,我已經使用以下服務器端編碼進行了一些小試驗:
int port = 8080;
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
while(true) {
if (s == null) {
/* nothing to do */
try {
wait();
} catch (InterruptedException e) {
continue;
}
}
try {
InputStream is = new BufferedInputStream(s.getInputStream());
PrintStream ps = new PrintStream(s.getOutputStream());
ANDROID_DEVICE = ps.flush();
}catch(IOException e){}
}
有很多方法可以解決這個問題。 以下是一些。 1) 使用應用服務器並安裝接受來自安卓客戶端請求的網絡服務。 2)創建自己的服務器。 注冊一個遠程對象,該對象將接受來自 android 客戶端的請求。 通過使用 RMI(遠程方法調用),您可以實現客戶端服務器架構。 3) 使用 JMS ,如果它在 Android 上受支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.