use of net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse in project Osmand by osmandapp.
the class InAppPurchaseHelper method sendTokens.
protected void sendTokens(@NonNull final List<PurchaseInfo> purchaseInfoList, final OnRequestResultListener listener) {
final String userId = ctx.getSettings().BILLING_USER_ID.get();
final String token = ctx.getSettings().BILLING_USER_TOKEN.get();
final String email = ctx.getSettings().BILLING_USER_EMAIL.get();
try {
String url = "https://osmand.net/subscription/purchased";
String userOperation = "Sending purchase info...";
final List<Request> requests = new ArrayList<>();
for (PurchaseInfo info : purchaseInfoList) {
Map<String, String> parameters = new HashMap<>();
parameters.put("userid", userId);
parameters.put("sku", info.getSku());
parameters.put("orderId", info.getOrderId());
parameters.put("purchaseToken", info.getPurchaseToken());
parameters.put("email", email);
parameters.put("token", token);
addUserInfo(parameters);
requests.add(new Request(url, parameters, userOperation, true, true));
}
AndroidNetworkUtils.sendRequestsAsync(ctx, requests, new OnSendRequestsListener() {
@Override
public void onRequestSending(@NonNull Request request) {
}
@Override
public void onRequestSent(@NonNull RequestResponse response) {
}
@Override
public void onRequestsSent(@NonNull List<RequestResponse> results) {
for (RequestResponse rr : results) {
String sku = rr.getRequest().getParameters().get("sku");
PurchaseInfo info = getPurchaseInfo(sku);
if (info != null) {
updateSentTokens(info);
String result = rr.getResponse();
if (result != null) {
try {
JSONObject obj = new JSONObject(result);
if (obj.has("error")) {
complain("SendToken Error: " + obj.getString("error") + " (response=" + result + " google=" + info.toString() + ")");
}
} catch (JSONException e) {
logError("SendToken", e);
complain("SendToken Error: " + (e.getMessage() != null ? e.getMessage() : "JSONException") + " (response=" + result + " google=" + info.toString() + ")");
}
}
}
}
if (listener != null) {
listener.onResult("OK", null, null);
}
}
private void updateSentTokens(@NonNull PurchaseInfo info) {
String tokensSentStr = ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.get();
Set<String> tokensSent = new HashSet<>(Arrays.asList(tokensSentStr.split(";")));
tokensSent.add(info.getSku());
ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.set(TextUtils.join(";", tokensSent));
}
@Nullable
private PurchaseInfo getPurchaseInfo(String sku) {
for (PurchaseInfo info : purchaseInfoList) {
if (info.getSku().equals(sku)) {
return info;
}
}
return null;
}
});
} catch (Exception e) {
logError("SendToken Error", e);
if (listener != null) {
listener.onResult("Error", null, null);
}
}
}
use of net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse in project Osmand by osmandapp.
the class BaseDeleteFilesCommand method onPostExecute.
@Override
protected void onPostExecute(Object o) {
List<OnDeleteFilesListener> listeners = getListeners();
if (!listeners.isEmpty()) {
Map<RemoteFile, String> errors = new HashMap<>();
for (DeleteRemoteFileTask task : tasks) {
boolean success;
String message = null;
RequestResponse response = task.response;
if (response != null) {
String errorStr = response.getError();
if (!Algorithms.isEmpty(errorStr)) {
message = new BackupError(errorStr).toString();
success = false;
} else {
String responseStr = response.getResponse();
try {
JSONObject result = new JSONObject(responseStr);
if (result.has("status") && "ok".equals(result.getString("status"))) {
success = true;
} else {
message = "Unknown error";
success = false;
}
} catch (JSONException e) {
message = "Json parsing error";
success = false;
}
}
if (!success) {
errors.put(task.remoteFile, message);
}
}
}
for (OnDeleteFilesListener listener : listeners) {
listener.onFilesDeleteDone(errors);
}
}
if (listener != null) {
getListeners().remove(listener);
}
}
Aggregations