gpt4 book ai didi

android - 如何仅从 HTTPResponse RestApi Android 获取字符串

转载 作者:行者123 更新时间:2023-11-30 02:12:09 25 4
gpt4 key购买 nike

我已经尝试使用此代码 fragment 来使用 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>

我只想不成功或成功

我必须在服务器端进行更改还是可以在客户端完成以获取纯文本

最佳答案

您需要的是多一行 - 当您成为 HttpResponse 时,它​​是来自响应站点的整个 html,因此您需要从中删除所有标签,您可以用一行 String responseAsText = android.text.Html.fromHtml(result).toString() 其中 result 是带有来自 Httpresponse 的响应的字符串。您可以在您提供的两个代码的末尾添加这一行。

关于android - 如何仅从 HTTPResponse RestApi Android 获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29936731/

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