artiya4u / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

New ObjectParser for "just" string responses. #242

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here's what I'm doing to get that..... but you'd implement it better of course 
...

public class StringResponse implements HttpRequestInitializer {
        public void initialize(HttpRequest request) {
            request.setParser(new XmlObjectParser(new XmlNamespaceDictionary()) {
                public <T> T parseAndClose(InputStream inputStream, Charset charset, Class<T> tClass) throws IOException {
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(inputStream, writer, charset.name());
                    return (T) writer.toString();
                }
            });
        }
    }

...

String data = new NetHttpTransport()
                    .createRequestFactory(new StringResponse())
                    .buildGetRequest(new GenericUrl("http://foobar.com/xyz"))
                    .execute().parseAs(String.class);

I appreciate that the benefit of your library is easy XML and JSON binding, but 
there's no reason not to supply a "String" version too.

Original issue reported on code.google.com by paul@hammant.org on 13 Aug 2013 at 5:09

GoogleCodeExporter commented 9 years ago
Hi Paul. Why not simply use the parseAsString() method?

String data = request.execute().parseAsString();

https://code.google.com/p/google-http-java-client/source/browse/google-http-clie
nt/src/main/java/com/google/api/client/http/HttpResponse.java?name=1.16.0-rc#509

Original comment by nev...@gmail.com on 3 Sep 2013 at 8:44