use of com.android.volley.DefaultRetryPolicy in project Logdata-Android by AndroidLogData.
the class HttpService method requestCrashData.
public void requestCrashData(String apiKey, CrashReportData report, final HttpCallback callback) {
String url = makeURL(TransferType.CRASH);
HttpOption option = httpOptionSetting(apiKey);
JSONObject jsonObject = makeCrashDataJSON(report);
JsonCustomRequest request = new JsonCustomRequest(Request.Method.POST, url, option, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.i("Response", response.get("result").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError", "Crash Transfer Error");
}
});
request.setRetryPolicy(new DefaultRetryPolicy(2000, 5, 1));
addRequestQueue(request);
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class FetchIDProfile method getProfile.
/**
* Method to get an enrollment id.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link ProfileItem} object. If the request was unsuccessful,
* the second parameter may be null.
*/
public Pair<Boolean, ProfileItem> getProfile() {
if (DEBUG) {
MyLog.i(CLS_NAME, "getProfile");
}
final long then = System.nanoTime();
String url = null;
try {
url = FETCH_URL + URLEncoder.encode(profileId, ENCODING);
} catch (final UnsupportedEncodingException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfile: UnsupportedEncodingException");
e.printStackTrace();
}
}
final RequestFuture<JSONObject> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
FetchIDProfile.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(JSON_HEADER_ACCEPT, JSON_HEADER_VALUE_ACCEPT);
params.put(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
return params;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
JSONObject response = null;
try {
response = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfile: InterruptedException");
e.printStackTrace();
}
} catch (final ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfile: ExecutionException");
e.printStackTrace();
}
} catch (final TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfile: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response.toString());
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final ProfileItem profileItem = gson.fromJson(response.toString(), ProfileItem.class);
if (DEBUG) {
MyLog.i(CLS_NAME, "response: getId: " + profileItem.getId());
}
return new Pair<>(true, profileItem);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class ListIDProfiles method getProfiles.
/**
* Method to get all enrollment profile information.
*
* @return an {@link Pair} of which the first parameter will denote success and the second an
* {@link ProfileList} object, containing the list of profiles. If the request was unsuccessful,
* the second parameter may be null.
*/
public Pair<Boolean, ProfileList> getProfiles() {
if (DEBUG) {
MyLog.i(CLS_NAME, "getProfiles");
}
final long then = System.nanoTime();
final RequestFuture<String> future = RequestFuture.newFuture();
final RequestQueue queue = Volley.newRequestQueue(mContext);
queue.start();
final StringRequest request = new StringRequest(Request.Method.GET, LIST_URL, future, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onErrorResponse: " + error.toString());
ListIDProfiles.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(OCP_SUBSCRIPTION_KEY_HEADER, apiKey);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * OTT_MULTIPLIER, 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, "getProfiles: InterruptedException");
e.printStackTrace();
}
} catch (final ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfiles: ExecutionException");
e.printStackTrace();
}
} catch (final TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "getProfiles: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
if (response != null) {
if (DEBUG) {
MyLog.i(CLS_NAME, "response: " + response);
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final Type type = new TypeToken<List<ProfileItem>>() {
}.getType();
final ProfileList profileList = new ProfileList(gson.<List<ProfileItem>>fromJson(response, type));
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: profileList size: " + profileList.getItems().size());
}
return new Pair<>(true, profileList);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "response: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class BingTranslateAPI method execute.
/**
* Perform a synchronous translation request
*
* @param ctx the application context
* @param token the Bing OAuth refresh token
* @param text the text to be translated
* @param from the {@link TranslationLanguageBing} to translate from
* @param to the {@link TranslationLanguageBing} to translate to
* @return a {@link Pair} with the first parameter donating success and the second the result
*/
public Pair<Boolean, String> execute(@NonNull final Context ctx, final String token, final String text, final TranslationLanguageBing from, final TranslationLanguageBing to) {
if (DEBUG) {
MyLog.i(CLS_NAME, "execute");
}
final long then = System.nanoTime();
final String params;
try {
params = PARAM_FROM_LANGUAGE + URLEncoder.encode(from.getLanguage(), ENCODING) + PARAM_TO_LANGUAGE + URLEncoder.encode(to.getLanguage(), ENCODING) + PARAM_TEXT + URLEncoder.encode(text, ENCODING);
} catch (final UnsupportedEncodingException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: UnsupportedEncodingException");
e.printStackTrace();
}
return new Pair<>(false, null);
}
final RequestFuture<String> future = RequestFuture.newFuture();
final Cache cache = UtilsVolley.getCache(ctx);
final Network network = new BasicNetwork(new HurlStack());
final RequestQueue queue = new RequestQueue(cache, network);
queue.start();
final String url = SERVICE_URL + params;
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());
BingTranslateAPI.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put("Content-Type", HEADER_CONTENT_TYPE);
params.put("Accept-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 translation = "";
try {
translation = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: InterruptedException");
e.printStackTrace();
}
} catch (ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: ExecutionException");
e.printStackTrace();
}
} catch (TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: TimeoutException");
e.printStackTrace();
}
} finally {
queue.stop();
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
if (UtilsString.notNaked(translation) && !translation.contains(ARGUMENT_EXCEPTION)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "translation: " + translation);
}
try {
translation = new GsonBuilder().disableHtmlEscaping().create().fromJson(translation, String.class);
} catch (final JsonSyntaxException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "translation: JsonSyntaxException");
e.printStackTrace();
}
}
return new Pair<>(true, translation);
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "translation: failed");
}
return new Pair<>(false, null);
}
}
use of com.android.volley.DefaultRetryPolicy in project Saiy-PS by brandall76.
the class BingOAuth method execute.
/**
* Get the Bing OAuth refresh token and store in {@link BingCredentials}
*
* @param ctx the application context
* @param synchronous whether or not this request should execute synchronously
* @return true if the process succeeded. False otherwise
*/
public boolean execute(@NonNull final Context ctx, final boolean synchronous) {
if (DEBUG) {
MyLog.i(CLS_NAME, "execute");
}
final long then = System.nanoTime();
final String encodedSubscriptionKey;
try {
encodedSubscriptionKey = URLEncoder.encode(MicrosoftConfiguration.MS_TRANSLATE_SUBSCRIPTION_KEY, ENCODING);
} catch (final UnsupportedEncodingException e) {
if (DEBUG) {
MyLog.i(CLS_NAME, "execute: UnsupportedEncodingException");
e.printStackTrace();
}
return false;
}
final RequestQueue queue = Volley.newRequestQueue(ctx);
queue.start();
Response.Listener<String> listener;
RequestFuture<String> future = null;
if (synchronous) {
if (DEBUG) {
MyLog.i(CLS_NAME, "synchronous: true");
}
future = RequestFuture.newFuture();
listener = future;
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "synchronous: false");
}
listener = new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: " + response);
}
queue.stop();
final BingCredentials credentials = new BingCredentials(response, 600);
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: subscription_key: " + credentials.getRefreshToken());
MyLog.i(CLS_NAME, "onResponse: expires_in: " + credentials.getExpires());
}
SPH.setBingToken(ctx, credentials.getRefreshToken());
SPH.setBingTokenExpiryTime(ctx, (System.currentTimeMillis() + (credentials.getExpires() * 1000)));
}
};
}
final StringRequest request = new StringRequest(Request.Method.POST, BING_OAUTH_URL, listener, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onErrorResponse: " + error.toString());
BingOAuth.this.verboseError(error);
}
queue.stop();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> params = new HashMap<>();
params.put(PARAM_CONTENT_TYPE, HEADER_CONTENT_TYPE);
params.put(PARAM_ACCEPT_CHARSET, ENCODING);
params.put(OCP_SUBSCRIPTION_KEY_HEADER, encodedSubscriptionKey);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);
String jsonResponse;
if (synchronous) {
try {
jsonResponse = future.get(THREAD_TIMEOUT, TimeUnit.SECONDS);
final BingCredentials credentials = new BingCredentials(jsonResponse, 600);
SPH.setBingToken(ctx, credentials.getRefreshToken());
SPH.setBingTokenExpiryTime(ctx, (System.currentTimeMillis() + (credentials.getExpires() * 1000)));
if (DEBUG) {
MyLog.i(CLS_NAME, "onResponse: subscription_key: " + credentials.getRefreshToken());
MyLog.i(CLS_NAME, "onResponse: expires_in: " + credentials.getExpires());
MyLog.getElapsed(CLS_NAME, then);
}
return true;
} catch (final JsonSyntaxException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: JsonSyntaxException");
e.printStackTrace();
}
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: InterruptedException");
e.printStackTrace();
}
} catch (ExecutionException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: ExecutionException");
e.printStackTrace();
}
} catch (TimeoutException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: TimeoutException");
e.printStackTrace();
}
} catch (final NullPointerException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: NullPointerException");
e.printStackTrace();
}
} catch (final Exception e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "execute: Exception");
e.printStackTrace();
}
} finally {
queue.stop();
}
}
if (DEBUG) {
MyLog.getElapsed(BingOAuth.class.getSimpleName(), then);
}
return false;
}
Aggregations