package com.apicall;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
public class QuestionProAPIExample {
public static final String API_ACCESS_KEY = "abcde";
public static final String API_URL = "http://api.questionpro.com/a/api/";
public static final String RESPONSE_COUNT_METHOD_NAME = "questionpro.survey.responseCount";
public static final String SURVEY_RESPONSES_METHOD_NAME = "questionpro.survey.surveyResponses";
public long surveyID = 3190054;
public long getResponseCount() throws Exception {
String path = API_URL + RESPONSE_COUNT_METHOD_NAME + "?accessKey=" + API_ACCESS_KEY;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
String data = "{\"id\":\"" + surveyID + "\",\"resultMode\":\"0\"}";
StringEntity se = new StringEntity(data);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
Object resp = httpclient.execute(httpost, responseHandler);
String json = resp.toString();
JSONObject obj = new JSONObject(json);
obj = obj.getJSONObject("response");
return Long.parseLong(obj.getString("partialCount"));
}
public String getResponses(int startingResponseCounter) throws Exception {
String path = API_URL + SURVEY_RESPONSES_METHOD_NAME + "?accessKey=" + API_ACCESS_KEY;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
String data = "{\"id\":\"" + surveyID + "\",\"resultMode\":\"0\",\"startDate\":\"08/15/2012\"," +
"\"endDate\":\"09/15/2012\",\"startingResponseCounter\":\""+ startingResponseCounter +"\"}";
StringEntity se = new StringEntity(data);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
Object resp = httpclient.execute(httpost, responseHandler);
return resp.toString();
}
public void printResponses() throws Exception {
long totalResponses = getResponseCount();
int count = 0;
int pageIndex = 0;
do {
String responsesJson = getResponses(pageIndex);
JSONObject jr = new JSONObject(responsesJson);
jr = jr.getJSONObject("response");
if (pageIndex == 0) {
System.out.println("Survey : " + jr.getString("id"));
JSONObject stats = jr.getJSONObject("surveyStatistics");
System.out.println("Viewed : " + stats.getString("viewedCount") +
" Started : " + stats.getString("startedCount") +
" Completed : " + stats.getString("completedCount"));
} else {
System.out.println("--------------------------------------------------------");
}
JSONArray responses = jr.getJSONArray("responses");
for (int i=0; i < responses.length();i++) {
JSONObject obj = (JSONObject)responses.get(i);
System.out.println("\nRESPONSE ID : " + obj.getString("id"));
JSONArray responseSet = obj.getJSONArray("responseSet");
for (int j=0;j < responseSet.length();j++) {
JSONObject questionResponse = (JSONObject)responseSet.get(j);
System.out.println("\n\t" + (j+1) + ". " + questionResponse.getString("questionText"));
System.out.println((questionResponse.has("questionDescription") ? "\n\t" +
" " + questionResponse.getString("questionDescription") : ""));
JSONArray answerValues = questionResponse.getJSONArray("values");
for (int k=0; k < answerValues.length();k++) {
JSONObject value = (JSONObject)answerValues.get(k);
System.out.println("\n\t\tANSWER ID : " + value.getString("id"));
System.out.println("\n\t\tANSWER TEXT : " + value.getString("answerText"));
JSONObject ansVal = value.getJSONObject("value");
String scale = (String) (ansVal != null ? ansVal.has("scale") ? ansVal.get("scale") : null : null);
String otherText = (String) (ansVal != null ? ansVal.has("other") ? ansVal.get("other") : null : null) ;
String dynamicText = (String) (ansVal != null ? ansVal.has("dynamic") ? ansVal.get("dynamic") : null :null);
String valTxt = (String) (ansVal != null ? ansVal.has("text") ? ansVal.get("text") : null : null);
String dateTimeText = (String) (ansVal != null ? ansVal.has("dateTime") ? ansVal.get("dateTime") : null : null);
String resultText = (String) (ansVal != null ? ansVal.has("result") ? ansVal.get("result") : null : null);
String fileLinkText = (String) (ansVal != null ? ansVal.has("fileLink") ? ansVal.get("fileLink") : null : null);
if (scale != null)
System.out.println("\n\t\tAnswer Scale is : " + scale);
//Incase the answer options included Other Option
if (otherText != null)
System.out.print("\n\t\tOther Text recorded is" + otherText);
//Incase of dynamic textbox
if (dynamicText != null)
System.out.print("\n\t\tDynamic Text recorded is" + dynamicText);
//Stored Answer Text
if (valTxt != null)
System.out.print("\n\t\tAnswer Text is" + valTxt);
//for date time questions
if (dateTimeText != null)
System.out.print("\n\t\tDate Time recorded is" + dateTimeText);
//For Constant Sum and Rank Order questions
if (resultText != null)
System.out.print("\n\t\tResult recorded is" + resultText);
// For File Upload questions
if (fileLinkText != null)
System.out.print("\n\t\tFile href recorded is" + fileLinkText);
}
}
}
pageIndex++;
count = pageIndex * 100;
} while (count < totalResponses);
}
}