开发者

Is there a function that strips HTTP response headers?

HttpResponse.getEntity().getContent() returns everything开发者_如何学Go... including response headers, javascript code, response body (of course!) etc.

Is there a function that cleans this up and provides only the response body?


You have to read the data from the InputStream to a buffer. Search for this regex:

\r\n\r\n(.*)

This will give you the things after the header.

Or you can replace it with an empty string, if you are searching for:

^.*?\r\n\r\n


You can filter out patterns using Android with your own method. Pass in the String, and apply pattern filters to remove things you don't want.

public String filter(String searchString)
    {
        String content = searchString;
        // Remove the new line characters.
        Pattern newLineChar = Pattern.compile("\n+");
        Matcher mLine = newLineChar.matcher(content);
        while (mLine.find())
            content = mLine.replaceAll("");
        // Return the clean content
        return content;
    }

You can get quite complicated with your patterns, and filter pretty much any expression you want. (You may need to use regular expressions etc). The example above replaces a new line (\n) with 0 length string, to remove all of them from the string. You can build up patterns, or you can just iterate through again to remove something else.

You will also need a couple of imports for this to work:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜