Skip to content

Instantly share code, notes, and snippets.

@gidili
Created August 30, 2012 13:42
Show Gist options
  • Save gidili/3528776 to your computer and use it in GitHub Desktop.
Save gidili/3528776 to your computer and use it in GitHub Desktop.
invoke servlet and transform json string into object
// 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