mardi 29 décembre 2015

Call update on access token

I have a database helper class that stores tokens. In another class I have an async method that does an http post. The token expires when I do the http post so the http post response in invalid.

I assume that I will need updating logic in my POST method which I have attempted below.

Any suggestions of how to continually update the token so the response is valid?

Pertinent database helper methods:

 public String getToken(){
        String token = "";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from " + TOKEN_TABLE_NAME, null);
        if(res.moveToFirst()) {
            token = res.getString(res.getColumnIndex("token"));
        }
        return token;
    }

    public Cursor getTokenRes(){
        String token = "";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from " + TOKEN_TABLE_NAME, null);
        return res;
    }

    public Boolean setToken(String token){
        Cursor oldTokenRes = getTokenRes();

        if(oldTokenRes.moveToFirst()) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(TOKEN.get("TOKEN").getColumnName(), token);
            int updateval = db.update(TOKEN_TABLE_NAME, contentValues, "_id = ? ", new String[]{Integer.toString(oldTokenRes.getInt(oldTokenRes.getColumnIndex("_id")))});
            return true;
        } else {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(TOKEN.get("TOKEN").getColumnName(), token);
            db.insert(TOKEN_TABLE_NAME, null, contentValues);

            return true;
        }
    }

    public Boolean setToken(JSONObject tokenjson){

        String token = tokenjson.optString("public_token");
        Cursor oldTokenRes = getTokenRes();

        if(oldTokenRes.moveToFirst()) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(TOKEN.get("TOKEN").getColumnName(), token);
            db.update(TOKEN_TABLE_NAME, contentValues, "_id = ? ", new String[]{Integer.toString(oldTokenRes.getInt(oldTokenRes.getColumnIndex("token")))});
            return true;
        } else {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(TOKEN.get("TOKEN").getColumnName(), token);
            db.insert(TOKEN_TABLE_NAME, null, contentValues);

            return true;
        }
    }

Pertinent POST methods:

 public String POST(String url) {
       String response = "";
       Looper.prepare();

       DatabaseHelper db = DatabaseHelper.getInstance(getApplicationContext());
       SharedPreferences prefs = getApplicationContext().getSharedPreferences("preferences", Context.MODE_PRIVATE);
       String token = prefs.getString("token", db.getToken());
       Log.v("token1", token);


        JSONObject tokenjson = null;
        token = tokenjson.optString("public_token");
        db.setToken(tokenjson);
       Log.v("token2", token);
       Log.v("tokenjson", tokenjson.toString());



       WebConnection conn = new WebConnection(URL + "?token=" + token, getApplicationContext());
       Log.v("URL", URL + "?token=" + token);
       conn.addValuePair("name", "subject");
       conn.addValuePair("message", "message");

       Log.v("conn", conn.toString());

       InputStream in = null;
       try {
           Utils utils = new Utils();
           in = conn.connect();
           Log.v("in", in.toString());
           response = utils.convertStreamToString(in);

       } catch (IOException e) {
           e.printStackTrace();
       }
       Log.v("response", response.toString());
       return response;
   }


    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String ...urls) {
            return POST(urls[0]);
        }

        //onPostExecute displays the results of the AsyncTask.
        @Override
            protected void onPostExecute(String response) {
            Log.v("onpost result", response);
            Toast.makeText(getBaseContext(), "Received.", Toast.LENGTH_LONG).show();
        }
    }

Aucun commentaire:

Enregistrer un commentaire