mercredi 30 décembre 2015

Database not updating with tokens

I have a login activity that grants an access token. I get an http post response from a url corresponding to the token. The only way I can get a response with an error is by updating the access token. However, the access token is not updating in the database.

public void updateToken(String token) {

            Cursor oldTokenRes = getTokenRes();
        if (oldTokenRes.moveToFirst()) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(TOKEN.get("TOKEN").getColumnName(), token);
            Log.v("cv1", token.toString());
            Log.v("update id", Integer.toString(oldTokenRes.getInt(oldTokenRes.getColumnIndex("_id"))));

            db.update(TOKEN_TABLE_NAME, contentValues, "_id = ? ", new String[]{Integer.toString(oldTokenRes.getInt(oldTokenRes.getColumnIndex("_id")))});
        }
    }

public String POST(String url) {

       String response = "";

       if (Looper.myLooper() == null) {
           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);
        String t = prefs.getString("token", token);
        Log.v("t", t);

       if (token != null) {

           String token1;
           token1 = db.getToken();

           db.updateToken(token1);
           Log.v("token1", token1); //not called, but calls update method's log

           uWebConnection conn = new uWebConnection(URL + "?token=" + token1, getApplicationContext());
           Log.v("URL", URL + "?token=" + token1);

           conn.addValuePair("alert_name", "subject");
           conn.addValuePair("alert_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(), "Alert URL Received.", Toast.LENGTH_LONG).show();
           }
       }

Any suggestions of how to alert the database of an updated token?

Aucun commentaire:

Enregistrer un commentaire