Android手机服务器HTTP响应的状态码打印在响应正文中
我正在使用Android 我使用以下代码设置响应:Android手机服务器HTTP响应的状态码打印在响应正文中,android,httpresponse,core,Android,Httpresponse,Core,我正在使用Android 我使用以下代码设置响应: HttpResponse getResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found"); getResponse.setEntity(new StringEntity(new String("The requested resource " + target + " could not be found due to mismatch!!"))); c
HttpResponse getResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
getResponse.setEntity(new StringEntity(new String("The requested resource " + target + " could not be found due to mismatch!!")));
conn.sendResponseHeader(getResponse);
conn.sendResponseEntity(getResponse);
我在Mozilla海报或浏览器中的回复标题为404,回复正文为:
The requested resource could not be found due to mismatch!!HTTP/1.1 200 OK
如何仅获取HTTP正文字符串?为什么我得到HTTP/1.1200 OK的响应。我没有在我的代码中设置这个。感谢您的帮助
使用。一行代码。有关使用它的Android项目示例,请参阅和
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/user");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
JSONObject response=new JSONObject(responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/user");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httppost,responseHandler);
JSONObject response=新的JSONObject(ResponseBy);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
在-添加本文和完整的HttpClient的组合,这可能会帮助您。。。
我认为响应
是您所需要的
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
我定义的handle()方法中存在问题。我正在为每个请求创建一个新的HttpResponse,而不是使用
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
请提供到Apache CORE的链接。@trojanfoe更新了这个问题。我开发了一个HTTP服务器。对于从浏览器获取请求,我只想显示HTTP正文。这应该在服务器端完成吗?因为我没有使用execute()方法。我想删除响应头并在浏览器中只打印HTTP正文@ashish.nheaders=新的ArrayList();添加(新的BasicNameValuePair(“名字”、“我的名字”);您可以在此处发送空白标题而不是myName您可以发送空白值。。。