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);
}
}
Aggregations