gpt4 book ai didi

android - 在 Android 中处理 POST 请求

转载 作者:可可西里 更新时间:2023-11-01 16:39:11 27 4
gpt4 key购买 nike

我正在尝试在 Android 中实现一个简单的 HTTP 服务器,它使用 DefaultHttpServerConnection 处理 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() 中的执行 block 。使用的客户端代码:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("STH", "do sth!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);

抱歉这个菜鸟问题。

最佳答案

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

关于android - 在 Android 中处理 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com