HttpURLConnection: how to read the answer
I'm going nuts on this. (as most people posting here lol)
I have a server written by somebody else and I'm sending reques开发者_如何学编程t to it to retrieve info.
I know that it works because I have some webpages that the other programmer used to test.
I would like to ask the question to the guy directly but since he lives in Japan and my in Spain its a bit complicated to communicate
my request works fine and I get the right response code but then I'm stuck, I don't know how to read the HttpURLConnection object to get the content.
in the web browser I get simply :
DOC_FOUND
ID
5
AUTHOR
adrian
TITLE
the adrian
DESCRIPTION
I tried getHeaderFields and getInputStream with no luck (I might be using them wrong)
Any ideas ?
Jason
Wrap your input stream with some kind of reader.
BufferedReader buf = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
Without code the best I can do is point you to a tutorial of how to properly connect. Maybe you will notice your error:
how-open-url-read-contents-httpurl-connection-java
Have you considered a simple wrapper around URLConnection that does all the heavy-lifting for you? With Resty it could be as simple as:
new Resty().text(url).toString();
if your content is plain text;
** DISCLAIMER: I'm the author of this free, open-source library. **
精彩评论