简体   繁体   English

http响应中的垃圾值

[英]Garbage value in http response

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 即时通讯工作在Android上,从http获取响应我的xml代码包含很多垃圾值在tahe结束..我如何删除垃圾...请建议

my xml string with garbage value is 我的垃圾值的xml字符串是

<player><id>1</id><name>sachin</name><value>98</value></player>ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ

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

and this is the code from where im accessing values : 这是我访问值的代码:

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

    }

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 在我的http代码中我也给了字节长度,但它仍然给出了垃圾值..因为它它抛出空指针和字符串超出绑定异常..我的代码是

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

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. 您读取了一些文本,然后将整个byte[]转换为String ,即使您没有使用read -call填充整个byte[] Also, you ignore if there is any more data (ie if your content is more than 1024 bytes). 此外,如果有更多数据(即,如果您的内容超过1024字节),则忽略。

The simplest solution to this is to use an InputSource with the InputStream it self directly: 最简单的解决方案是直接使用InputSourceInputStream

xr.parse(new InputSource(in));

The SAX parser already knows how to handle an InputStream and does all of that for you. SAX解析器已经知道如何处理InputStream并为您完成所有这些操作。 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();

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 . 另请注意,将此转换为String非常简单,因为XML可能具有任何编码,您需要检测正确的编码才能将byte[]正确转换为String

you must get how much bytes you have read, and then truncate with this value. 您必须获取已读取的字节数,然后使用此值进行截断。

Also you can get Content-Length Header and use it to determine the length of body 您还可以获取Content-Length Header并使用它来确定身体的长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM