Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Hello

Querying the API
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
//get this org.json library from http://www.java2s.com/Code/JarDownload/java/java-json.jar.zip
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LeadLiaisonAPIClient {

    public static void main(String[] args) throws IOException, JSONException {
        // TODO Auto-generated method stub
        StringBuffer content = getRestResponse(
                "https://api-url/v1.0/visits.json?api_key=PUT_KEY_HERE&from_date=2018-03-01&include_prospect_info=1");

        JSONObject jsonObject = new JSONObject(content.toString());
        JSONArray records = (JSONArray) jsonObject.get("records");

        //ArrayList<Integer> vistorList = new ArrayList<Integer>();

        for (int i = 0; i < records.length(); i++) {
            //vistorList.add(records.getJSONObject(i).getInt("visitor_id"));
            StringBuffer hitRep = getRestResponse(
                    "https://api-url/v1.0/hits.json?api_key=PUT_KEY_HERE&visit_id="
                            + records.getJSONObject(i).getInt("visitor_id"));
            //System.out.println(hitRep);
        }

    }

    private static StringBuffer getRestResponse(String urlString)
            throws MalformedURLException, IOException, ProtocolException {
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        if (responseCode != 200)
            throw new RuntimeException("ResponseCode: " + responseCode);
        else {
            
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            con.disconnect();
            return content;
        }
    }
}
  • No labels