Search in sources :

Example 1 with JSONObject

use of com.nuance.dragon.toolkit.oem.api.json.JSONObject in project Saiy-PS by brandall76.

the class BVEmotionAnalysis method getAnalysis.

/**
 * Method to get a temporary access token.
 *
 * @return an {@link Pair} of which the first parameter will denote success and the second an
 * {@link BVCredentials} object, containing the token credentials. If the request was unsuccessful,
 * the second parameter may be null.
 */
public Pair<Boolean, Emotions> getAnalysis(@NonNull final String recordingId, final int offset) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getAnalysis");
    }
    final RequestFuture<String> future = RequestFuture.newFuture();
    final Cache cache = UtilsVolley.getCache(mContext);
    final Network network = new BasicNetwork(new HurlStack());
    final RequestQueue queue = new RequestQueue(cache, network);
    queue.start();
    final String url = ANALYSIS_URL + recordingId + FROM_MS + String.valueOf(offset);
    final StringRequest request = new StringRequest(Request.Method.GET, url, future, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(final VolleyError error) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
                BVEmotionAnalysis.this.verboseError(error);
            }
            queue.stop();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            final Map<String, String> params = new HashMap<>();
            params.put(CHARSET, ENCODING);
            params.put(AUTHORIZATION, BEARER_ + token);
            return params;
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(request);
    String response = null;
    try {
        response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
    } catch (final InterruptedException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "execute: InterruptedException");
            e.printStackTrace();
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "execute: ExecutionException");
            e.printStackTrace();
        }
    } catch (final TimeoutException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "execute: TimeoutException");
            e.printStackTrace();
        }
    } finally {
        queue.stop();
    }
    if (response != null) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onResponse: " + response);
            try {
                final JSONObject object = new JSONObject(response);
                MyLog.i(CLS_NAME, "object: " + object.toString(4));
            } catch (final JSONException e) {
                e.printStackTrace();
            }
        }
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        final Emotions emotions = gson.fromJson(response, Emotions.class);
        emotions.setRecordingId(recordingId);
        new AnalysisResultHelper(mContext, sl).interpretAndStore(emotions);
        return new Pair<>(true, emotions);
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "onResponse: failed");
        }
        return new Pair<>(false, null);
    }
}
Also used : HashMap(java.util.HashMap) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) Gson(com.google.gson.Gson) RequestQueue(com.android.volley.RequestQueue) Network(com.android.volley.Network) BasicNetwork(com.android.volley.toolbox.BasicNetwork) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Pair(android.util.Pair) VolleyError(com.android.volley.VolleyError) HurlStack(com.android.volley.toolbox.HurlStack) GsonBuilder(com.google.gson.GsonBuilder) StringRequest(com.android.volley.toolbox.StringRequest) JSONException(org.json.JSONException) AnalysisResultHelper(ai.saiy.android.cognitive.emotion.provider.beyondverbal.AnalysisResultHelper) Emotions(ai.saiy.android.cognitive.emotion.provider.beyondverbal.analysis.Emotions) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) BasicNetwork(com.android.volley.toolbox.BasicNetwork) JSONObject(com.nuance.dragon.toolkit.oem.api.json.JSONObject) Cache(com.android.volley.Cache)

Aggregations

AnalysisResultHelper (ai.saiy.android.cognitive.emotion.provider.beyondverbal.AnalysisResultHelper)1 Emotions (ai.saiy.android.cognitive.emotion.provider.beyondverbal.analysis.Emotions)1 Pair (android.util.Pair)1 Cache (com.android.volley.Cache)1 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)1 Network (com.android.volley.Network)1 NetworkResponse (com.android.volley.NetworkResponse)1 RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 BasicNetwork (com.android.volley.toolbox.BasicNetwork)1 HurlStack (com.android.volley.toolbox.HurlStack)1 StringRequest (com.android.volley.toolbox.StringRequest)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JSONObject (com.nuance.dragon.toolkit.oem.api.json.JSONObject)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JSONException (org.json.JSONException)1