【发布时间】:2018-08-03 18:58:29
【问题描述】:
我在 Android 示例中有简单的 http 服务器。我喜欢这个服务器,所以我也想从浏览器接收 POST 数据。我怎样才能用标准的东西(没有外部库)来接收它?我尝试像 GET 一样接收它,但是 js 控制台显示连接错误。
private void handle(Socket socket) throws IOException {
BufferedReader reader = null;
PrintStream output = null;
try {
String route = null;
// Read HTTP headers and parse out the route.
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while (!TextUtils.isEmpty(line = reader.readLine())) {
if (line.startsWith("GET /")) {
int start = line.indexOf('/') + 1;
int end = line.indexOf(' ', start);
route = line.substring(start, end);
break;
}
}
// Output stream that we send the response to
output = new PrintStream(socket.getOutputStream());
// Prepare the content to send.
if (null == route) {
writeServerError(output);
return;
}
byte[] bytes = loadContent(route);
if (null == bytes) {
writeServerError(output);
return;
}
// Send out the content.
output.println("HTTP/1.0 200 OK");
output.println("Content-Type: " + detectMimeType(route));
output.println("Content-Length: " + bytes.length);
output.println();
output.write(bytes);
output.flush();
} finally {
if (null != output) {
output.close();
}
if (null != reader) {
reader.close();
}
}
}
【问题讨论】:
-
你能把你要解析的json或数据的结构贴出来吗?
-
@GastónSaillén 它只是没有 json 的文本数据
-
I have simple http server on android from Android Samples.请提供链接,以便我们查看您正在使用的内容。 -
@greenapps 嘿,查看完整的代码链接。