如何仅从 HTTPResponse RestApi 获取字符串 Android
How to get only string from HTTPResponse RestApi Android
我已尝试使用此代码片段从 android 使用 HTTPResponse
获取响应
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>
我只想不成功或成功
我是否必须在服务器端进行更改,或者可以在客户端完成以获取纯文本
您需要的是多一行 - 当您成为 HttpResponse 时,它是来自响应站点的整个 html,因此您需要从中删除所有标签,您可以用一行来完成String responseAsText = android.text.Html.fromHtml(result).toString()
其中 result
是带有来自 Httpresponse 的响应的字符串。您可以在您提供的两个代码的末尾添加这一行。
我已尝试使用此代码片段从 android 使用 HTTPResponse
获取响应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>
我只想不成功或成功
我是否必须在服务器端进行更改,或者可以在客户端完成以获取纯文本
您需要的是多一行 - 当您成为 HttpResponse 时,它是来自响应站点的整个 html,因此您需要从中删除所有标签,您可以用一行来完成String responseAsText = android.text.Html.fromHtml(result).toString()
其中 result
是带有来自 Httpresponse 的响应的字符串。您可以在您提供的两个代码的末尾添加这一行。