Created
August 30, 2012 13:42
-
-
Save gidili/3528776 to your computer and use it in GitHub Desktop.
invoke servlet and transform json string into object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// servlet is local to the app | |
String requestURL = request.getRequestURL().toString(); | |
String servletPath = request.getServletPath(); | |
String serverPath = requestURL.substring(0,requestURL.indexOf(servletPath)); | |
URL url = new URL(serverPath + "/getScores"); | |
URLConnection conn = url.openConnection(); | |
conn.setConnectTimeout(12000); | |
InputStream is = conn.getInputStream(); | |
StringWriter writer = new StringWriter(); | |
IOUtils.copy(is, writer, "UTF-8"); | |
String jsonStr = writer.toString(); | |
Gson gson = new Gson(); | |
Type listType = new TypeToken<List<ScoreRecord>>() {}.getType(); | |
List<ScoreRecord> scores = gson.fromJson(jsonStr, listType); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment