fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import com.google.gson.Gson;
  8. import com.google.gson.reflect.TypeToken;
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. SomeObjectType result;
  15. String urlString;
  16. // works
  17. Type returnType = new TypeToken<SomeObjectType>() {
  18. }.getType();
  19. result = (SomeObjectType) Utils.urlToGson(urlString,returnType);
  20.  
  21. // fails, unable to cast someInternalListType to SomeObjectType
  22. result = Utils.urlToGson(urlString);
  23. }
  24.  
  25. public static <T> T urlToGson(String urlString) {
  26. CloseableHttpClient httpclient = HttpClients.createDefault();
  27. HttpGet httpGet = new HttpGet(urlString);
  28. CloseableHttpResponse response1 = httpclient.execute(httpGet);
  29.  
  30. Type returnType = new TypeToken<T>() {
  31. }.getType();
  32.  
  33. HttpEntity entity1 = response1.getEntity();
  34.  
  35. Gson gson = new Gson(); // Or use new GsonBuilder().create();
  36. return (T)gson.fromJson(EntityUtils.toString(entity1), returnType); // deserializes
  37. }
  38.  
  39. public static Object urlToGson(String urlString, Type returnType) {
  40. CloseableHttpClient httpclient = HttpClients.createDefault();
  41. HttpGet httpGet = new HttpGet(urlString);
  42. CloseableHttpResponse response1 = httpclient.execute(httpGet);
  43. HttpEntity entity1 = response1.getEntity();
  44.  
  45. Gson gson = new Gson(); // Or use new GsonBuilder().create();
  46. return gson.fromJson(EntityUtils.toString(entity1), returnType); // deserializes
  47. }
  48.  
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
Main.java:8: error: package com.google.gson.reflect does not exist
import com.google.gson.reflect.TypeToken;
                              ^
Main.java:39: error: cannot find symbol
	public static Object urlToGson(String urlString, Type returnType) {
	                                                 ^
  symbol:   class Type
  location: class Ideone
Main.java:14: error: cannot find symbol
		SomeObjectType result;
		^
  symbol:   class SomeObjectType
  location: class Ideone
Main.java:17: error: cannot find symbol
		Type returnType = new TypeToken<SomeObjectType>() {
		^
  symbol:   class Type
  location: class Ideone
Main.java:17: error: cannot find symbol
		Type returnType = new TypeToken<SomeObjectType>() {
		                      ^
  symbol:   class TypeToken
  location: class Ideone
Main.java:17: error: cannot find symbol
		Type returnType = new TypeToken<SomeObjectType>() {
		                                ^
  symbol:   class SomeObjectType
  location: class Ideone
Main.java:19: error: cannot find symbol
		result = (SomeObjectType) Utils.urlToGson(urlString,returnType);
		          ^
  symbol:   class SomeObjectType
  location: class Ideone
Main.java:19: error: cannot find symbol
		result = (SomeObjectType) Utils.urlToGson(urlString,returnType);
		                          ^
  symbol:   variable Utils
  location: class Ideone
Main.java:22: error: cannot find symbol
		result =  Utils.urlToGson(urlString);
		          ^
  symbol:   variable Utils
  location: class Ideone
Main.java:26: error: cannot find symbol
		CloseableHttpClient httpclient = HttpClients.createDefault();
		^
  symbol:   class CloseableHttpClient
  location: class Ideone
Main.java:26: error: cannot find symbol
		CloseableHttpClient httpclient = HttpClients.createDefault();
		                                 ^
  symbol:   variable HttpClients
  location: class Ideone
Main.java:27: error: cannot find symbol
			HttpGet httpGet = new HttpGet(urlString);
			^
  symbol:   class HttpGet
  location: class Ideone
Main.java:27: error: cannot find symbol
			HttpGet httpGet = new HttpGet(urlString);
			                      ^
  symbol:   class HttpGet
  location: class Ideone
Main.java:28: error: cannot find symbol
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			^
  symbol:   class CloseableHttpResponse
  location: class Ideone
Main.java:30: error: cannot find symbol
				Type returnType = new TypeToken<T>() {
				^
  symbol:   class Type
  location: class Ideone
Main.java:30: error: cannot find symbol
				Type returnType = new TypeToken<T>() {
				                      ^
  symbol:   class TypeToken
  location: class Ideone
Main.java:33: error: cannot find symbol
				HttpEntity entity1 = response1.getEntity();
				^
  symbol:   class HttpEntity
  location: class Ideone
Main.java:35: error: cannot find symbol
				Gson gson = new Gson(); // Or use new GsonBuilder().create();
				^
  symbol:   class Gson
  location: class Ideone
Main.java:35: error: cannot find symbol
				Gson gson = new Gson(); // Or use new GsonBuilder().create();
				                ^
  symbol:   class Gson
  location: class Ideone
Main.java:36: error: cannot find symbol
				return (T)gson.fromJson(EntityUtils.toString(entity1), returnType); // deserializes
				                        ^
  symbol:   variable EntityUtils
  location: class Ideone
Main.java:40: error: cannot find symbol
		CloseableHttpClient httpclient = HttpClients.createDefault();
		^
  symbol:   class CloseableHttpClient
  location: class Ideone
Main.java:40: error: cannot find symbol
		CloseableHttpClient httpclient = HttpClients.createDefault();
		                                 ^
  symbol:   variable HttpClients
  location: class Ideone
Main.java:41: error: cannot find symbol
			HttpGet httpGet = new HttpGet(urlString);
			^
  symbol:   class HttpGet
  location: class Ideone
Main.java:41: error: cannot find symbol
			HttpGet httpGet = new HttpGet(urlString);
			                      ^
  symbol:   class HttpGet
  location: class Ideone
Main.java:42: error: cannot find symbol
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			^
  symbol:   class CloseableHttpResponse
  location: class Ideone
Main.java:43: error: cannot find symbol
				HttpEntity entity1 = response1.getEntity();
				^
  symbol:   class HttpEntity
  location: class Ideone
Main.java:45: error: cannot find symbol
				Gson gson = new Gson(); // Or use new GsonBuilder().create();
				^
  symbol:   class Gson
  location: class Ideone
Main.java:45: error: cannot find symbol
				Gson gson = new Gson(); // Or use new GsonBuilder().create();
				                ^
  symbol:   class Gson
  location: class Ideone
Main.java:46: error: cannot find symbol
				return gson.fromJson(EntityUtils.toString(entity1), returnType); // deserializes
				                     ^
  symbol:   variable EntityUtils
  location: class Ideone
30 errors
stdout
Standard output is empty