Create json object from http request using apache http - Java Network

Java examples for Network:apache http

Description

Create json object from http request using apache http

Demo Code


import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetBugList {

    public static void main(String[] args) throws IOException, IOException,
            JSONException {/*from  w  w  w  . j  a v a 2s  .c om*/
        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {

            HttpGet httpget = new HttpGet(
                    "https://your host");
            System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                @Override
                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils
                                .toString(entity) : null;
                    } else {
                        throw new ClientProtocolException(
                                "Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = httpclient.execute(httpget,
                    responseHandler);
            //System.out.println(responseBody);

            JSONArray collectionATraiter = new JSONArray();

            JSONObject jsoEntity = new JSONObject(responseBody);
            JSONObject jsoResult = jsoEntity.getJSONObject("result");
            JSONArray jsaBugs = jsoResult.getJSONArray("bugs");

            for (int i = 0; i < jsaBugs.length(); i++) {
                JSONObject jsoIterator = jsaBugs.getJSONObject(i);
                int id = jsoIterator.getInt("id");
                String whiteboard = jsoIterator.getString("whiteboard");

                if (whiteboard.equals("Before Processing By Bonita")) {
                    System.out.println(id);
                    whiteboard = " ";
                    System.out.println(whiteboard.isEmpty());
                }

            }

        } finally {
            httpclient.close();
        }
    }
}

Related Tutorials