Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are many HTTP client libraries, but given that you're pulling in JSON your best bet is to use the Jersey client library. You need to create a Java object which matches the JSON (in this case, a <code>Response</code> object which contains a field <code>Docs</code> which is an array of <code>Data</code> objects or similar) and tell the Jersey client to expect this as a result. Then you will be able to work with Java objects to output it in whatever form you wish.</p> <p>*<em>Update</em></p> <p>Basic overview of the code. First, set up the Jersey client:</p> <pre><code>import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.client.WebResource; .... final ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); final Client client = Client.create(clientConfig); </code></pre> <p>Then create your request:</p> <pre><code>final WebResource webResource = client.resource("http://mydomain.com/myresource"); </code></pre> <p>At this stage, if you want to fetch your JSON back as a <code>String</code> you can just call:</p> <pre><code>final String json = webResource.get(String.class); </code></pre> <p>But the real benefit of using Jersey over the other HTTP clients is that it will parse the JSON for you so that you don't need to think about it. If you create the following classes:</p> <pre><code>public class DataResponse { private final List&lt;DataDoc&gt; docs; @JsonCreator public DataResponse(@JsonProperty("docs")List&lt;DataDocs&gt; docs) { this.docs = docs; } public List&lt;DataDoc&gt; getDocs() { return this.docs; } } public class DataDoc { final String id; final String name; // Other fields go here @JsonCreator public DataDoc(@JsonProperty("id") String id, @JsonProperty("name") String name) { this.id = id; this.name = name; } // Getters go here } </code></pre> <p>then you can change your Jersey client code to:</p> <pre><code>final DataResponse response = webResource.get(DataResponse.class); </code></pre> <p>and you can now access the fields in response as per a normal Java object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload