在 Android 应用程序中从 Java SocketServer 接收字符串
Receiving a String from a Java SocketServer in an Android App
感谢您花时间阅读我的问题。
我目前正在开发我的第一个 Android 应用程序。我正在尝试从我的 Java SocketServer 发送一个字符串,它在 VPS 上不断地 运行 到 android 应用程序。当我在 Eclipse 中编写一个 10 行的客户端时,它工作得很好。我现在的问题是如何在 Android 中获得响应,因为我收到类似 "You can't use Sockets in the main thread" 的错误,但我想对其进行编码的方式是使用一种将响应作为字符串返回的方法。
也许我应该补充一点,我的服务器响应没有提供 input/String。我只想打开连接并从输入流中读取字符串。
我发送的服务器代码:
try {
OutputStream out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String toSend = "";
//This is just some serialization of own Elements to a string.
boolean first = true;
for (VertretungsPlanElement ele : Main.Elemente) {
if (!first) {
toSend = toSend + "~";
}
first = false;
toSend = toSend + ele.toString();
}
//Serialization ends
pw.println(toSend);
pw.flush();
pw.close();
out.flush();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
当我尝试从我的非 Android 客户端获取响应时,此方法有效,但当我尝试在 Android.[=14 中获取它时,它给了我 null 或 "" =]
我的android代码:
try {
client = new Socket(hostname, port);
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
response = is.readLine();
Vertretungsplan.Cards.add(new VertretungsplanCard("Response:", response));
} catch (Exception e) {}
我现在做了差不多一天的研究,我读了一些关于 "AsynchronousTasks" 的东西。
如果这个问题已经被问到,我很抱歉。
感谢您抽出时间提供帮助!
是的,等等 Android 线程模型意味着 "main thread" 是您的应用通常 运行 所处的,除了执行您编写的代码外,它还执行绘制到屏幕的代码。因此,如果您在主线程上进行网络连接,您的应用程序将变得无响应,因此框架会采取保护措施以尽早发现并阻止它。
您需要在另一个线程上进行联网。我通常不会推荐 ASyncTask
作为线程解决方案,但对于作为初学者的您来说,这是让您入门的最佳选择。
http://developer.android.com/training/multiple-threads/index.html
http://developer.android.com/reference/android/os/AsyncTask.html
ExampleTask task = new ExampleTask();
task.execute();
private class ExampleTask extends AsyncTask<Void, Void, String> {
@Override
protected Long doInBackground(Void... notUsed) {
try {
client = new Socket(hostname, port);
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
return is.readLine();
} catch (Exception e) {
return "failed " + e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
Vertretungsplan.Cards.add(new VertretungsplanCard("Response:", result));
}
}
感谢您花时间阅读我的问题。
我目前正在开发我的第一个 Android 应用程序。我正在尝试从我的 Java SocketServer 发送一个字符串,它在 VPS 上不断地 运行 到 android 应用程序。当我在 Eclipse 中编写一个 10 行的客户端时,它工作得很好。我现在的问题是如何在 Android 中获得响应,因为我收到类似 "You can't use Sockets in the main thread" 的错误,但我想对其进行编码的方式是使用一种将响应作为字符串返回的方法。
也许我应该补充一点,我的服务器响应没有提供 input/String。我只想打开连接并从输入流中读取字符串。
我发送的服务器代码:
try {
OutputStream out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String toSend = "";
//This is just some serialization of own Elements to a string.
boolean first = true;
for (VertretungsPlanElement ele : Main.Elemente) {
if (!first) {
toSend = toSend + "~";
}
first = false;
toSend = toSend + ele.toString();
}
//Serialization ends
pw.println(toSend);
pw.flush();
pw.close();
out.flush();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
当我尝试从我的非 Android 客户端获取响应时,此方法有效,但当我尝试在 Android.[=14 中获取它时,它给了我 null 或 "" =]
我的android代码:
try {
client = new Socket(hostname, port);
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
response = is.readLine();
Vertretungsplan.Cards.add(new VertretungsplanCard("Response:", response));
} catch (Exception e) {}
我现在做了差不多一天的研究,我读了一些关于 "AsynchronousTasks" 的东西。
如果这个问题已经被问到,我很抱歉。
感谢您抽出时间提供帮助!
是的,等等 Android 线程模型意味着 "main thread" 是您的应用通常 运行 所处的,除了执行您编写的代码外,它还执行绘制到屏幕的代码。因此,如果您在主线程上进行网络连接,您的应用程序将变得无响应,因此框架会采取保护措施以尽早发现并阻止它。
您需要在另一个线程上进行联网。我通常不会推荐 ASyncTask
作为线程解决方案,但对于作为初学者的您来说,这是让您入门的最佳选择。
http://developer.android.com/training/multiple-threads/index.html http://developer.android.com/reference/android/os/AsyncTask.html
ExampleTask task = new ExampleTask();
task.execute();
private class ExampleTask extends AsyncTask<Void, Void, String> {
@Override
protected Long doInBackground(Void... notUsed) {
try {
client = new Socket(hostname, port);
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
return is.readLine();
} catch (Exception e) {
return "failed " + e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
Vertretungsplan.Cards.add(new VertretungsplanCard("Response:", result));
}
}