我已经尝试过这段代码片段来使用HTTPResponse
从android获取响应String s="";
try {
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://10.0.0.8:7777/HttpPostServlet/servlet/Login");
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("uname", valuse[0]));
list.add(new BasicNameValuePair("pwd",valuse[1]));
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse httpResponse= httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
String responseStr = EntityUtils.toString(httpResponse.getEntity());
s= readResponse(httpResponse);
System.err.println("Response: " + s +"/// "+ "another resposn :: "+responseStr );
} catch(Exception exception) {
System.err.println("Exception: " + exception.getMessage());
}
我也试过另一种方法来获得回应
public String readResponse(HttpResponse res) {
InputStream is=null;
String return_text="";
String result="";
try {
is=res.getEntity().getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
String line="";
StringBuffer sb=new StringBuffer();
HttpEntity entity = res.getEntity();
while ((line=bufferedReader.readLine())!=null) {
sb.append(line);
}
return_text=sb.toString();
result = EntityUtils.toString(entity).toString();
} catch (Exception e) {}
return result;
}
我得到的答复如下
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://23.253.164.20:8096/">Unsuccessfull</string>
我只希望不成功或成功
我是否必须在服务器端进行更改,或者可以在客户端进行更改以获取纯文本
答案 0 :(得分:1)
您需要的是另外一行 - 当您成为HttpResponse时,它是来自响应站点的整个html,因此您需要从中删除所有标记,并且您可以使用单行String responseAsText = android.text.Html.fromHtml(result).toString()
执行此操作result
是您的字符串,其中包含来自Httpresponse的repsonse。您可以在所提供的两个代码的末尾添加此行。