Reputation: 80
I'm trying to implement a simple HTTP server in Android that handles POST requests, with DefaultHttpServerConnection, but my problem is in receiving the enclosed Entity.
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);
}
And the handleUpload method:
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);
}
The execution blocks in EntityUtils.toString(). The client code used:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("STH", "do sth!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Sorry for the noob question.
Upvotes: 2
Views: 1327
Reputation: 4014
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
conn.bind(serverSocket.accept(), new BasicHttpParams());
HttpRequest request = conn.receiveRequestHeader();
conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
System.out.println(EntityUtils.toString(entity));
http://hc.apache.org/httpcomponents-core-ga/tutorial/pdf/httpcore-tutorial.pdf
Upvotes: 1