2
我想在Android中使用DefaultHttpServerConnection實現一個簡單的HTTP服務器,但是我的問題在於接收封閉的實體。在Android中處理POST請求
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
conn.bind(socket, new BasicHttpParams());
HttpRequest request = conn.receiveRequestHeader();
String method = request.getRequestLine().getMethod().toUpperCase();
if (method.equals("POST")) {
if (DEBUG) Log.d(TAG, "POST received");
handleUpload(conn, request);
}
而且handleUpload方法:
private void handleUpload(DefaultHttpServerConnection conn, HttpRequest request) throws HttpException, IOException {
HttpResponse response = new BasicHttpResponse(new HttpVersion(1,1), 200, "OK");
BasicHttpEntityEnclosingRequest enclosingRequest = new BasicHttpEntityEnclosingRequest(request.getRequestLine());
conn.receiveRequestEntity(enclosingRequest);
if (DEBUG) Log.d(TAG, "Before input");
String r = EntityUtils.toString(enclosingRequest.getEntity());
if (DEBUG) Log.d(TAG, "Entity: " + r);
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
}
在EntityUtils.toString執行塊()。客戶使用的代碼:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("STH", "do sth!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
對不起noob問題。