Garbage value in http response

2019-07-29 03:58发布

问题:

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

my xml string with garbage value is

<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

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

回答1:

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. Also, you ignore if there is any more data (i.e. if your content is more than 1024 bytes).

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

xr.parse(new InputSource(in));

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

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.



回答2:

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