在处理Android的POST请求 [英] Handling POST requests in Android
本文介绍了在处理Android的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现Android的处理POST请求,与DefaultHttpServerConnection一个简单的HTTP服务器,但我的问题是在接受封闭的实体。
DefaultHttpServerConnection康恩=新DefaultHttpServerConnection();
conn.bind(插座,新BasicHttpParams());
HTT prequest请求= conn.receiveRequestHeader();字符串的方法= request.getRequestLine()实现getMethod()与toUpperCase()。
如果(method.equals(POST)){
如果(调试)Log.d(TAG,POST接到);
handleUpload(康涅狄格州,请求);
}
而handleUpload方式:
私人无效handleUpload(康涅狄格州DefaultHttpServerConnection,Htt的prequest要求)抛出HttpException,IOException异常{
HTT presponse响应=新BasicHtt presponse(新HttpVersion(1,1),200,OK);
BasicHttpEntityEnclosingRequest enclosingRequest =新BasicHttpEntityEnclosingRequest(request.getRequestLine());
conn.receiveRequestEntity(enclosingRequest);
如果(调试)Log.d(TAG,输入之前);
串R = EntityUtils.toString(enclosingRequest.getEntity());
如果(调试)Log.d(TAG,实体:+ R); conn.sendResponseHeader(响应);
conn.sendResponseEntity(响应);
}
在EntityUtils.toString执行块()。使用的客户端code:
列表<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(1);
nameValuePairs.add(新BasicNameValuePair(STH,做......!));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
对不起,noob问题。
解决方案
DefaultHttpServerConnection康恩=新DefaultHttpServerConnection();
conn.bind(的ServerSocket.accept(),新BasicHttpParams());
HTT prequest请求= conn.receiveRequestHeader();
conn.receiveRequestEntity((HttpEntityEnclosingRequest)要求);
HttpEntity实体=((HttpEntityEnclosingRequest)要求).getEntity();
的System.out.println(EntityUtils.toString(实体));
的http://hc.apache.org/httpcomponents-core-ga/tutorial/pdf/httpcore-tutorial.pdf
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.
解决方案
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的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文