【问题标题】:How to get only string from HTTPResponse RestApi Android如何从 HTTPResponse RestApi Android 仅获取字符串
【发布时间】:2015-04-29 07:14:20
【问题描述】:

我已尝试使用此代码 sn-p 使用 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;
}

我收到如下回复

&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;string xmlns="http://23.253.164.20:8096/"&gt;Unsuccessfull&lt;/string&gt;

我只想要不成功或成功

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

【问题讨论】:

标签: android http-post httpresponse webmethod


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多