Java tutorial
/* * Copyright 2015 Tapcentive, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.tapcentive.sdk.service; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Base64; import android.util.Log; import com.android.volley.Request.Method; import com.android.volley.Response; import com.android.volley.VolleyError; import com.tapcentive.sdk.TapcentiveConfig; import com.tapcentive.sdk.TapcentiveLibrary; import com.tapcentive.sdk.error.TapcentiveError; import com.tapcentive.sdk.error.TapcentiveErrorType; import com.tapcentive.sdk.model.Reward.RewardStatusCallback; import com.tapcentive.sdk.service.util.CustomJsonObjectRequest; /** * The Class CouponStatusService is used to check the validity of a coupon retrieved by a tap */ public class CouponStatusService extends TapcentiveVolleyService { /** The Constant TAG. */ private static final String TAG = "CouponStatusService"; /** The callback. */ private RewardStatusCallback callback; private Context context; /** * Instantiates a new coupon status service. * * @param context the context * @param callback the callback */ public CouponStatusService(Context context, RewardStatusCallback callback) { super(context); this.context = context; this.callback = callback; } /** * Execute. * * @param coupon the coupon */ public void execute(byte[] coupon) { Map<String, String> headers = new HashMap<String, String>(); JSONObject jsonCoupon = new JSONObject(); if (isConnectedToNetwork()) { try { headers.put("User-Agent", "consumer"); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context); String msiURLString = sharedPref.getString("pref_key_msi_url", ""); if ((msiURLString != null) && (!msiURLString.equals(""))) { String url = msiURLString + "/reward/status"; jsonCoupon.put("timestamp", (long) System.currentTimeMillis() / 1000); jsonCoupon.put("apdu", Base64.encodeToString(coupon, Base64.NO_WRAP)); Log.d(TAG, "Coupon Data: " + Base64.encodeToString(coupon, Base64.NO_WRAP)); CustomJsonObjectRequest jsonRequest = new CustomJsonObjectRequest(Method.POST, url, jsonCoupon, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, "Redeemable" + response.toString()); callback.isValid(true); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d(TAG, "Error " + error.toString()); if (error == null || error.networkResponse == null) { callback.isValid(true); return; } if (error.networkResponse.statusCode == 400 || error.networkResponse.statusCode == 204 || error.networkResponse.statusCode == 403) { Response<JSONObject> response = CustomJsonObjectRequest.parseError(error); try { int detailcode = response.result.getInt("detailcode"); String extraDetails = response.result.getString("details"); Log.d(TAG, "Got an error: " + error.networkResponse.statusCode + " Detail Code from MSI: " + detailcode + " Extra Details: " + extraDetails); } catch (JSONException e) { e.printStackTrace(); } callback.isValid(false); /*TapcentiveConfig config = TapcentiveLibrary.getInstance().getConfig(); if(config.getCallback() != null) { config.getCallback().onTapcentiveError(new TapcentiveError(TapcentiveErrorType.TapcentiveUnexpectedError)); }*/ return; } } }, headers); // Add the request to the queue and execute getRequestQueue().add(jsonRequest); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d(TAG, "Exception" + e.toString()); callback.isValid(true); TapcentiveConfig config = TapcentiveLibrary.getInstance().getConfig(); if (config.getCallback() != null) { config.getCallback().onTapcentiveError( new TapcentiveError(this.context, TapcentiveErrorType.TapcentiveUnexpectedError)); } } } else { TapcentiveConfig config = TapcentiveLibrary.getInstance().getConfig(); if (config.getCallback() != null) { config.getCallback().onTapcentiveError( new TapcentiveError(this.context, TapcentiveErrorType.TapcentiveNetworkUnavailable)); } } } }