Java – Problems parsing JSON responses from Wikipedia

Problems parsing JSON responses from Wikipedia… here is a solution to the problem.

Problems parsing JSON responses from Wikipedia

So I’m making an android app to search the mediawiki api for a little bit of information about some celebrity.

The idea is that you can type in any name and it will provide the information that the mediawiki api has about that name/person, but for now I’m sticking with just one name until I figure out how to parse JSON correctly.

I need to extract the fields from this json response:
JSON response

That’s what I have so far, I think the problem is in the Query class, I just don’t know what the class needs to ensure that only extracted fields are returned. When I run the program, the output of the onResponse() method is only empty. Thanks for your help.

Ok, I made the changes as suggested, here’s my updated code :

;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Map;

public class game extends AppCompatActivity {

private static final String ENDPOINT = "https://en.wikipedia.org/w/api.php?    format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Harry%20Potter";

private RequestQueue requestQueue;

private Gson gson;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

requestQueue = Volley.newRequestQueue(this);

GsonBuilder gsonBuilder = new GsonBuilder();
    gson = gsonBuilder.create();
}

public void fetchPerson(View view)
{
    fetchPosts();
}
private void fetchPosts() {
    StringRequest request = new StringRequest(Request.Method.GET, ENDPOINT, onPostsLoaded, onPostsError);

requestQueue.add(request);
}

private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        final TextView text = (TextView) findViewById(R.id.textView);

Page page = gson.fromJson(response, Page.class);

text.setText(page.extract);

}
};

private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("PostActivity", error.toString());
    }
};
public class Root {
    String batchcomplete;
    Query  query;
}
public class Query {
    Map<String, Page> pages;
}
public class Page {
    int    pageid;
    int    ns;
    String title;
    String extract;
}

Solution

I think the problem is within the Query class

You are right.

The JSON data is as follows:

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "23140032": {
        "pageid": 23140032,
        "ns": 0,
        "title": "Frodo Baggins",
        "extract": "Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales."
      }
    }
  }
}

The root object has 2 fields: batchcomplete and query.

You tried to parse an object that has the following fields: extract and title.

Do you see the difference there?

If you want to use Gson, you need classes for all objects.

class Root {
    String batchcomplete;
    Query  query;
}
class Query {
    Map<String, Page> pages;
}
class Page {
    int    pageid;
    int    ns;
    String title;
    String extract;
}

Update

You need to parse the JSON to the root object.

Sample code:

String json = "{\n" +
              "  \"batchcomplete\": \"\",\n" +
              "  \"query\": {\n" +
              "    \"pages\": {\n" +
              "      \"23140032\": {\n" +
              "        \"pageid\": 23140032,\n" +
              "        \"ns\": 0,\n" +
              "        \"title\": \"Frodo Baggins\",\n" +
              "        \"extract\": \"Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales.\"\n" +
              "      }\n" +
              "    }\n" +
              "  }\n" +
              "}";
Root root = new Gson().fromJson(json, Root.class);
for (Page page : root.query.pages.values()) {
    System.out.println(page.title);
    System.out.println("  " + page.extract);
}

Output

Frodo Baggins
  Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales.

Related Problems and Solutions