Android Open Source - SurveySDK_android Rest Client






From Project

Back to project page SurveySDK_android.

License

The source code is released under:

Apache License

If you think the Android project SurveySDK_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.survey.android.webclient;
// w  w w .j  av  a 2  s.c o  m
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.survey.android.model.AnswerModel;
import com.survey.android.util.ConstantData;
import com.survey.android.util.Log;
import com.survey.android.util.WhiteLabel;

public class RestClient {
  public static final String STACK_TRACE_EP = new SurveyRequest("stacktrace",
      new HashMap<String, String>()).toString();
  private static final String TAG = "RestClient";

  public static JSONArray getCategories(String token) throws JSONException,
      IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    return wrapJSONArray(new SurveyRequest("categories.json", hm).get());
  }

  public static String getCurrentSection(String token, String id)
      throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "responses");
    hm.put("id", id);
    return (new SurveyRequest("current_section", hm).get());
  }

  public static JSONObject getResponses(String token, String id)
      throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "surveys");
    hm.put("id", id);
    return wrapJSONObject(new SurveyRequest("responses.json", hm).post());
  }

  public static JSONArray getSurveys(String token, String id)
      throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "categories");
    hm.put("id", id);
    return wrapJSONArray(new SurveyRequest("surveys.json", hm).get());
  }
  
  public static JSONObject getSurveyById(String token, String surveyId) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "surveys");
    hm.put("id", surveyId);
    return wrapJSONObject(new SurveyRequest("", hm).get());
  }

  public static boolean RemoveSurveyById(String token, String surveyId) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "surveys");
    hm.put("id", surveyId);
    String result = new SurveyRequest("", hm).delete();
    if (result.equals("true")) {
      return true;
    } else {
      return false;
    }  
  }

  public static boolean setAnswers(String token, String id,
      Map<String, String> hm) throws JSONException, IOException {
    hm.put("token", token);
    hm.put("model_name", "responses");
    hm.put("id", id);
    String result = new SurveyRequest("answers.json", hm).post();
    if (result.equals("true")) {
      return true;
    } else {
      return false;
    }
  }

  public static JSONObject signIn(JSONObject userJSONObject)
      throws IOException, Exception {
    String result = new SurveyRequest("sessions", userJSONObject.toString())
        .post();
    try {
      return wrapJSONObject(result);
    } catch (JSONException e) {
      throw new Exception(result);
    }
  }
  
  public static JSONObject signInOrRegister(JSONObject userJSONObject)
  throws IOException, Exception {
    String result = new SurveyRequest("sessionSdk", userJSONObject.toString())
    .post();
    try {
      return wrapJSONObject(result);
    } catch (JSONException e) {
      throw new Exception(result);
    }
  }
  
  public static JSONObject cashOut(String token,String bodyJSON)
      throws IOException, Exception {
    Map<String,String> hm=new HashMap<String,String>();
    hm.put("token",token);
    String result = new SurveyRequest("users/payouts",hm,bodyJSON )
        .post();
    try {
      return wrapJSONObject(result);
    } catch (JSONException e) {
      throw new Exception(result);
    }
  }

  public static JSONObject signUp(JSONObject userJSONObject)
      throws JSONException, IOException {
    return wrapJSONObject(new SurveyRequest("users",
        userJSONObject.toString()).post());
  }
  
  public static JSONObject editProfile(JSONObject userJSONObject,String token,String id)throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "users");
    hm.put("id", id);
    return wrapJSONObject(new SurveyRequest("",hm,
        userJSONObject.toString()).post());
  }

  /**
   * 
   * @param the string to wrap
   * @return the JSONarray encapsulating the string data
   * @throws JSONException
   * @throws IOException
   */
  public static JSONArray wrapJSONArray(String result) throws JSONException,
      IOException {
    return new JSONArray(result);
  }

  /**
   * 
   * @param the string to wrap
   * @return the JSONObject encapsulating the string data
   * @throws JSONException
   * @throws IOException
   */
  public static JSONObject wrapJSONObject(String result)
      throws JSONException, IOException {
    return new JSONObject(result);
  }

  public static boolean sendAnswerResults(String token, String id,
      List<AnswerModel> answers) throws JSONException, IOException {
    String model_name = "responses";
    AnswerModel a = new AnswerModel("token", "TOKEN", token);
    answers.add(0, a);
    boolean result = new SurveyRequest("answers", token, id, model_name,
        answers).post(true);
    return result;
  }
  
  public static JSONObject getUserByToken(String token,String id)throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "users");
    hm.put("id", id);
    return wrapJSONObject(new SurveyRequest("", hm).get());
  }
  
  public static JSONObject getUserIdByToken(String token)throws JSONException, IOException {
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "users");
    hm.put("id","id");
    JSONObject object = wrapJSONObject(new SurveyRequest("", hm).get()); 
    return object;
  }
  
  public static JSONObject getEarnings(String token, String id) throws JSONException, IOException {
    Map<String, String> hm=new HashMap<String, String>();
    hm.put("token", token);
    hm.put("model_name", "users");
    hm.put("id", id);
    return wrapJSONObject(new SurveyRequest("earnings", hm).get());
  }
  
  public static JSONObject sendC2DMRegistrationId(String registrationId, String type, String deviceid, String token, String userId) throws JSONException, IOException {
    JSONObject object = new JSONObject();
    object.put("gcm_notification_token", registrationId);
    object.put("device_type", type);
    object.put("device_id", deviceid);    
    JSONObject temp=new JSONObject();
    temp.put("user", object);
    
    if (ConstantData.WHITE_LABEL_APP.isWhiteLabel(WhiteLabel.SURVEY)) {
      Log.d(TAG, "gcm_notification_token: " + registrationId);
      Log.d(TAG, "device_type: " + type);
      Log.d(TAG, "device_id: " + deviceid);      
      Log.d(TAG, "userId: " + userId);
      Log.d(TAG, "token: " + token);
    }

    return wrapJSONObject(new SurveyRequest("users/"+userId+"?token="+token,temp.toString()).post());
  }

  public static JSONArray pollGeoSurveys(String token, double latitude, double longitude) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("u_lat", Double.toString(latitude));
    hm.put("u_long", Double.toString(longitude));    
    return wrapJSONArray(new SurveyRequest("geosurveys/surveys", hm).get());    
  }
  
  public static String getGeoPush(String token, String geo_trigger_id) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("geo_trigger_id", geo_trigger_id);
    return new SurveyRequest("push.json", hm).get();    
  }
  
  public static String setLocationLog(String token, String geo_trigger_id, double latitude, double longitude, String reason) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("latitude", Double.toString(latitude));
    hm.put("longitude", Double.toString(longitude));
    hm.put("reason", reason);
    hm.put("geo_trigger_id", geo_trigger_id);    
    return new SurveyRequest("log", hm).get();    
  }
  
  public static JSONObject pollGeoSurvey(String token, double latitude, double longitude) throws JSONException, IOException{
    Map<String, String> hm = new HashMap<String, String>();
    hm.put("token", token);
    hm.put("u_lat", Double.toString(latitude));
    hm.put("u_long", Double.toString(longitude));      
    return wrapJSONObject(new SurveyRequest("geosurveys/surveys", hm).get());    
  }
  
  /**
   * fetching resources for AutoCompleteTextViews - country, state
   * @return
   * @throws IOException 
   * @throws JSONException 
   */
  public static JSONObject getRegistrationResources() throws JSONException, IOException{
    Map<String,String> temp=null;
    return wrapJSONObject(new SurveyRequest("users",temp).get());
  }
  
  public static JSONObject getAppVersionInfo() throws JSONException, IOException{
    Map<String, String> hm=null;
    return wrapJSONObject(new SurveyRequest("apps/android", hm).get());
  }
  
  @SuppressWarnings("unused")
  public static String changePassword(String email)throws IOException, JSONException{
//      Map<String,String>hm=new HashMap<String,String>();
//      hm.put("email", email);
  
    JSONObject result = wrapJSONObject(new SurveyRequest("users/reset_password", null,email).post());
    
    boolean success=false;
    boolean error_messages=false;
    
    String message="";

    try{
      if(result.has("success")){
        message+="Success.\nPlease, check your email.";
      }
      else if(result.has("error_messages")){
        JSONArray array=result.getJSONArray("error_messages");
        for(int i=0;i<array.length();i++){
          message+=array.getString(i)+"\n";
        }
      }
    }
    catch(Exception e){
      e.printStackTrace();
    }
    return message;
  }
  
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Dashboard.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.Notification.java
com.survey.android.view.Question.java
com.survey.android.view.Survey.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.DashboardThemed.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.NotificationThemed.java
com.survey.android.view.themed.QuestionThemed.java
com.survey.android.view.themed.SurveyThemed.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java