http响应中的垃圾值 [英] Garbage value in http response

查看:48
本文介绍了http响应中的垃圾值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 android 并从 http 获取响应,我的 xml 代码在 tahe 端包含大量垃圾值..我如何删除这些垃圾...请推荐

im working on android and on getting response from http my xml code contain lots of garbage value at tahe end.. how do i remove that garbage... please suggest

我的带有垃圾值的 xml 字符串是

my xml string with garbage value is

<代码><玩家GT;<编号→1</ID><名称> SACHIN</名称><值大于98</值GT;</播放器及GT;ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ

这是我创建数组并返回其值的代码

here is the code where im creating array and returning its values

    private ArrayList<String> id = new ArrayList<String>();
    private ArrayList<String> name = new ArrayList<String>();
    private ArrayList<String> score = new ArrayList<String>();
    private ArrayList<String> value = new ArrayList<String>();

    public ArrayList<String> getId() {
        return id;
    }

    public void setId(String id) {
        this.id.add(id);
    }

    public ArrayList<String> getName() {
        return name;
    }

    public void setName(String name) {
        this.name.add(name);
    }

    public ArrayList<String> getScore() {
        return score;
    }
    public void setScore(String score) {
        this.score.add(score);
    }

    public ArrayList<String> getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value.add(value);
    }

这是我访问值的代码:

Boolean currentElement = false;
String currentValue = null;
public static ScoreList scorelist = null;

public static ScoreList getScorelist() {
    return scorelist;
}

public static void setScoreList(ScoreList scorelist) {
    XMLHandler.scorelist = scorelist;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    currentElement = true;

  if (localName.equals("id"))
    {
        /** Start */
        scorelist = new ScoreList();
        }else if (localName.equals("id")) {
        }else if (localName.equals("name")) {
        }else if (localName.equals("score")) {
        }else if (localName.equals("value")) {
    }

    }

        @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

    currentElement = false;

            if (localName.equalsIgnoreCase("id"))
            scorelist.setId(currentValue);
        if (localName.equalsIgnoreCase("name"))
            scorelist.setName(currentValue);
        else if (localName.equalsIgnoreCase("score"))
            scorelist.setScore(currentValue);
        else if (localName.equalsIgnoreCase("value"))
            scorelist.setValue(currentValue);

    }

        @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }

在我的http代码中,我也给出了字节长度,但它仍然给出了垃圾值..因为它抛出空指针和字符串越界异常..我的代码是

here in my http code i gave the byte length also but still it is giving garbage values.. because of it it is throwing null pointer and string out of bound exception.. my code is

                UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
                httppost.setEntity(p_entity);
                HttpResponse response = client.execute(httppost);
                Log.v(TAG, response.getStatusLine().toString());
                HttpEntity responseEntity = response.getEntity();
                InputStream in=responseEntity.getContent();

                byte[] bData = new byte[1024];
                in.read(bData);
                System.out.println("In Data"+in.toString());
                String st=new String (bData);

                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                System.out.println("Response from server"+responseEntity);
                XMLHandler myXMLHandler = new XMLHandler();
                xr.setContentHandler(myXMLHandler);
                xr.parse(retrieveInputStream(responseEntity));

推荐答案

正如我所料.在这段代码中:

As I expected. In this code:

byte[] bData = new byte[1024];
in.read(bData);
System.out.println("In Data"+in.toString());
String st=new String (bData);

您阅读了一些文本,然后将整个 byte[] 转换为 String,即使您没有填充整个 byte[]read 调用.此外,如果有更多数据(即您的内容超过 1024 字节),您将忽略.

You read some text and then convert the whole byte[] into a String, even if you didn't fill the whole byte[] with the read-call. Also, you ignore if there is any more data (i.e. if your content is more than 1024 bytes).

最简单的解决方案是直接使用 InputSourceInputStream 自身:

The simplest solution to this is to use an InputSource with the InputStream it self directly:

xr.parse(new InputSource(in));

SAX 解析器已经知道如何处理 InputStream 并为您完成所有这些工作.无需重新实现该行为.

The SAX parser already knows how to handle an InputStream and does all of that for you. There no need to re-implement that behaviour.

如果您绝对必须在内存中读取整个响应,那么您需要使用这样的代码(但我不鼓励这样做):

If you absolutely must read the whole response in memory, then you'd need to use code like this (but I wouldn't encourage this):

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read;
while ((read=in.read(buf))!=-1) {
  baos.write(buf, 0, read);
}
byte[] output = baos.toByteArray();

另请注意,将其转换为 String 并非易事,因为 XML 可以具有任何编码,您需要检测正确的编码才能正确转换 byte[]String.

Also note that converting this to a String is non-trivial, as the XML could have any encoding and you'd need to detect the correct one to correctly convert the byte[] to a String.

这篇关于http响应中的垃圾值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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