What I want is this:
1)send a GET request to a WordPress site
2) Get the json response from the site
3) from that response (which I cannot know how or what elements it contains but I know for sure it contains at least one that interests me. That being “content”) get one element of the JSON and throw the rest.
I really cannot get this working. I have posted the simple code that I understand that is needed to get this working but I get a IllegalArgumentException
. Clearly I have misunderstood something in the library
URL hotelPresentationURL = new URL(
"http://rodosseavillas.gr/wp-json/posts/206");
HttpURLConnection conn = (HttpURLConnection) hotelPresentationURL
.openConnection();
conn.connect();
InputStream reader = conn.getInputStream();// optimize
JsonReader jsonReader = new JsonReader(
new InputStreamReader(reader));
jsonReader.setLenient(true);
jsonReader.beginObject();
JsonObject response = new Gson().fromJson(jsonReader,
JsonObject.class);
JsonElement element = response.get("content");
jsonReader.close();
Log.d(GuideFragment.TAG, "JSON RECEIVED: " + element.toString());
05-29 17:29:15.868: E/AndroidRuntime(2636): java.lang.RuntimeException: An error occured while executing doInBackground()
05-29 17:29:15.868: E/AndroidRuntime(2636): at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-29 17:29:15.868: E/AndroidRuntime(2636): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.lang.Thread.run(Thread.java:841)
05-29 17:29:15.868: E/AndroidRuntime(2636): Caused by: java.lang.IllegalArgumentException
05-29 17:29:15.868: E/AndroidRuntime(2636): at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:676)
05-29 17:29:15.868: E/AndroidRuntime(2636): at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642)
05-29 17:29:15.868: E/AndroidRuntime(2636): at com.google.gson.Gson.fromJson(Gson.java:810)
05-29 17:29:15.868: E/AndroidRuntime(2636): at com.hellobusiness.fragments.ContentFetcherGuide.doInBackground(GuideFragment.java:61)
05-29 17:29:15.868: E/AndroidRuntime(2636): at com.hellobusiness.fragments.ContentFetcherGuide.doInBackground(GuideFragment.java:1)
05-29 17:29:15.868: E/AndroidRuntime(2636): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-29 17:29:15.868: E/AndroidRuntime(2636): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-29 17:29:15.868: E/AndroidRuntime(2636): ... 4 more
here what i believe is your mistake, what you should be doing is that instead of using JsonObject you should be using proper dto, like this …
you can use the following site to generate the dto stucture pojo, this is a very simple examples of how you should be using GSON example 1, example 2, example 3