如何从HTT presponse RESTAPI的Andr​​oid只有字符串 [英] How to get only string from HTTPResponse RestApi Android

查看:236
本文介绍了如何从HTT presponse RESTAPI的Andr​​oid只有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过这code段采用摆脱Android的响应HTT presponse

I have tried this code snippet to get the response from android using 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());
}

我也有试过这个另一种方法来获得效应初探

also i have tried this another method to get reponse

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;
}

我得到的反应如下:

I am getting response as follows

&LT;?XML版本=1.0编码=UTF-8&GT;&LT;字符串的xmlns =htt​​p://23.253.164.20:8096/&GT; Unsuccessfull&LT; /串&GT;

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://23.253.164.20:8096/">Unsuccessfull</string>

我只希望unsuccessfull或成功

I want only unsuccessfull or successful

我要在服务器端更改或它可以做客户端获得纯文本

推荐答案

您需要的是多了一个行 - 当你成为了Htt presponse它是从响应网站全HTML,所以你需要删除所有从它的标签,你可以用一个行字符串responseAsText = android.text.Html.fromHtml(结果)的ToString(),其中结果做到这一点与从Htt的presponse的repsonse您的字符串。您可以在两个codeS您所提供的末尾添加这一行。

What you need is one more line - when you become the HttpResponse it is the whole html from the response site, so you need to remove all tags from it and you could do this with a single line String responseAsText = android.text.Html.fromHtml(result).toString() where result is your string with the repsonse from the Httpresponse. You can add this line at the end of both codes you have provided.

这篇关于如何从HTT presponse RESTAPI的Andr​​oid只有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆