我遇到的问题是拆分时的请求。它说请求无法解析为类型。
public class RequestHandler implements IRequestHandler {
private static final String NO_IMPL_GET_1_0 = "HTTP/1.0 501 GET Not Implemented\r\n\r\n";
private static final String NO_IMPL_HEAD_1_0 = "HTTP/1.0 501 HEAD Not Implemented\r\n\r\n";
private static final String NO_IMPL_POST_1_0 = "HTTP/1.0 501 POST Not Implemented\r\n\r\n";
private static final String BAD_REQUEST_1_0 = "HTTP/1.0 400 Bad Request\r\n\r\n";
private static final String BAD_REQUEST_0_9 = "HTTP/1.0 400 Bad Request\r\n\r\n";
@Override
public byte[] processRequest(byte[] request) {
// TODO Auto-generated method stub
String request1 = new String(request1);
String[] array1 = new request.split(" "); //Problem here with the request
String resp = null;
if (array1.length == 3 && array1[0].equals("GET")) {
resp = RequestHandler.NO_IMPL_GET_1_0;
System.out.println(resp);
} else if (array1[0].equals("HEAD")) {
resp = RequestHandler.NO_IMPL_HEAD_1_0;
System.out.println(resp);
} else if (array1[0].equals("POST")) {
resp = RequestHandler.NO_IMPL_POST_1_0;
System.out.println(resp);
} else {
resp = RequestHandler.BAD_REQUEST_1_0;
System.out.println(resp);
}
if (array1.length == 2 && array1[0].equals("GET")) {
resp = RequestHandler.BAD_REQUEST_0_9;
System.out.println(resp);
}
}
}
答案 0 :(得分:2)
这些行包含几个问题:
public byte[] processRequest(byte[] request) {
// TODO Auto-generated method stub
String request1 = new String(request1);
String[] array1 = new request.split(" ");
request1
String
实例初始化变量request1
。但你怎么能阅读"来自request1
(如果尚未创建新的字符串)?new
语句用于创建" new"某个类的实例,例如new String(...)
。您已使用变量而不是类名称来调用它。那无能为力。byte
数组。这不起作用,因为没有为此数据类型定义方法。我猜这里你的主要问题是错误的变量命名。您有request
和request1
,并且已在代码中切换它们。您使用了request1
,request
使用request
并使用了request1
代替public byte[] processRequest(byte[] request) {
// TODO Auto-generated method stub
String requestAsString = new String(request);
String[] requestParts = requestAsString.split(" ");
。
尝试为变量找到更好的名称,以便了解它们包含的内容以及应如何处理/使用它们:
{{1}}
这些可能不是最好的名字,但我想你明白了。
答案 1 :(得分:-2)
替换String[] array1 = new request.split(" ");
与
String[] array1 = request.split(" ");
那就是它。