我的要求是收到Android的采用Socket HTTP POST请求(内容是JSON文件)。 当我打印socket.getInputStream(),我得到:
POST/HTTP/1.1
Content length:41
Content-type: application/json
Host:10.0.2.15:5001
Connection: Keep Alive
User-Agent: Apache HTTPClient/Unavailable(Java 1.4)
CRLF(Carriage return line feed)-
Actual Contents of JSON<Data>
我使用的链接https://forums.oracle.com/forums/thread.jspa?messageID=7076709 ,但它打印唯一的“内容类型:应用/ JSON主机:10.0”(可用于41的长度)在logcat的。
我的代码是:
serverSocket = new ServerSocket(portNumber);
while(true){
socket = serverSocket.accept();
List<String> headers = new ArrayList<String>();
String str;
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((str = reader.readLine()) != null)
{
headers.add(str);
if (str.startsWith("Content-Length: "))
{
break; // and don't get the next line!
}
}
int contentLength = Integer.parseInt(headers.get(headers.size() - 1).substring("Content-Length: ".length()));
for(int j=0;j<headers.size();j++)
{
Log.d("Headers:",headers.get(j));
}
StringBuilder requestContent = new StringBuilder();
int ch;
for (int i = 0; i < contentLength; i++)
{
requestContent.append((char) reader.read());
}
Log.d("The request content is: ",""+requestContent);
这是我的输出:集管:POST / HTTP / 1.1的Content-Length:41
requestContent:内容类型:应用程序/ JSON主持人:10。
是否有人可以指导我如何能够做到? 提前致谢!