简体   繁体   中英

SharedPreferences not getting saved

So I have been trying to figure out a way to save the Access Token I get from my API. I can successfully get the JSON response from my API and store it in my result variable within my doInBackground .

However, for some reason it is not getting saved in SharedPreferences in my onPostExecute .

The result variable contains this JSON string {"access_token":"4Oq6o8oAGRf4oflu3hrbsy18qeIfG1","expires_in":36000,"token_type":"Bearer","scope":"read write","refresh_token":"iocSNJ2PTVbph2RnWmcf0Zv69PDKjw"} , which I received from my API.

I have an algorithm that is supposed to save only the access_token for now.

My code is below:

WSAdapter.java

public class WSAdapter {
    static public class SendAPIRequests extends AsyncTask<String, String, String> {
        // Add a pre-execute thing

        SharedPreferences ShPreference;
        SharedPreferences.Editor PrefEditor;
        static String MyPREFERENCES = "API Authentication";
        String accessToken = "Access Token";

        private WeakReference<Context> mLoginReference;

        // constructor
        public SendAPIRequests(Context context){
            mLoginReference = new WeakReference<>(context);
        }

        @Override
        protected String doInBackground(String... params) {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

            Log.e("TAG", params[0]);
            Log.e("TAG", params[1]);
            //String data = "";

            StringBuilder result = new StringBuilder();

            HttpURLConnection httpURLConnection = null;
            try {

                // Sets up connection to the URL (params[0] from .execute in "login")
                httpURLConnection = (HttpURLConnection) new URL(params[2]).openConnection();

                // Sets the request method for the URL
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                httpURLConnection.setRequestProperty("Accept","application/json");

                // Tells the URL that I am sending a POST request body
                httpURLConnection.setDoOutput(true);
                // Tells the URL that I want to read the response data
                httpURLConnection.setDoInput(true);

                // JSON object for the REST API
                JSONObject jsonParam = new JSONObject();
                jsonParam.put("client_id", "mYIHBd321Et3sgn7DqB8urnyrMDwzDeIJxd8eCCE");
                jsonParam.put("client_secret", "qkFYdlvikU4kfhSMBoLNsGleS2HNVHcPqaspCDR0Wdrdex5dHyiFHPXctedNjugnoTq8Ayx7D3v1C1pHeqyPh1BjRlBTQiJYSuH6pi9EVeuyjovxacauGVeGdsBOkHI3");
                jsonParam.put("username", params[0]);
                jsonParam.put("password", params[1]);
                jsonParam.put("grant_type", "password");

                Log.i("JSON", jsonParam.toString());

                // To write primitive Java data types to an output stream in a portable way
                DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
                // Writes out a byte to the underlying output stream of the data posted from .execute function
                wr.writeBytes(jsonParam.toString());
                // Flushes the jsonParam to the output stream
                wr.flush();
                wr.close();

                // // Representing the input stream
                InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                // reading the input stream / response from the url
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // Disconnects socket after using
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            }

            Log.e("TAG", result.toString());
            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {
            //super.onPostExecute(result);
            // expecting a response code fro my server upon receiving the POST data
            Log.e("TAG", result);

            // retrieves the context passed
            Context context = mLoginReference.get();

            ShPreference = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

            // edits shared preferences for authentication and authorization
            PrefEditor = ShPreference.edit();

            // to save the Access Token from the API
            try {
                JSONObject pJObject = new JSONObject(result);

            PrefEditor.putString(accessToken, pJObject.getString("access_token"));
            PrefEditor.apply();
            // algorithm for parsing the JSONArray from the Django REST API
            /*for (int i = 0; i < pJObjArray.length(); i++) {
                // puts the current iterated JSON object from the array to another temporary object
                JSONObject pJObj_data = pJObjArray.getJSONObject(i);
                PrefEditor.putString(accessToken, pJObj_data.getString("access_token"));
                PrefEditor.apply();
            }*/

            } catch (JSONException e) {
                //Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                Log.d("Json","Exception = "+e.toString());
            }
        }
    }

This code below includes the code that reads the SharedPreferences. Which should be in the doInBackground of this AsyncTask as I need to put the access_token to the header.

This is supposed to be in the same class.

public class SendPostsRequest extends AsyncTask<String, String, String> {
        TextView postsSect;
        // Add a pre-execute thing
        HttpURLConnection urlConnection;

        // gets the activity context
        private WeakReference<Context> mPostReference;
        // to be able to access activity resources
        Activity activity;

        SharedPreferences ShPreference;
        SharedPreferences.Editor PrefEditor;
        String accessToken = "Access Token";

        // constructor
        public SendPostsRequest(Context context, Activity activity){
            mPostReference = new WeakReference<>(context);
            this.activity = activity;
        }

        @Override
        protected String doInBackground(String... params) {

            StringBuilder result = new StringBuilder();

            // retrieves the context passed
            Context context = mPostReference.get();

            ShPreference = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

            String APIAuthentication = "Bearer " + ShPreference.getString(accessToken, "");

            try {
                // Sets up connection to the URL (params[0] from .execute in "login")
                urlConnection = (HttpURLConnection) new URL(params[0]).openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setRequestProperty ("Authorization", APIAuthentication);
                urlConnection.connect();

                InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

            }catch( Exception e) {
                e.printStackTrace();
            }
            finally {
                urlConnection.disconnect();
            }

            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {

            // expecting a response code fro my server upon receiving the POST data
            Log.e("TAG", result);

            // gets the JSON files stored in the posts details class from Posts Activity
            Posts.PostsDetails postsHelper = new Posts().new PostsDetails();

            // retrieves the context passed
            Context context = mPostReference.get();

            // For posts
            try {
                JSONArray pJObjArray = new JSONArray(result);

                // algorithm for parsing the JSONArray from the Django REST API
                for (int i = 0; i < pJObjArray.length(); i++) {
                    // puts the current iterated JSON object from the array to another temporary object
                    JSONObject pJObj_data = pJObjArray.getJSONObject(i);
                    // inputs necesarry elements to the ListPosts function
                    postsHelper.setPost(pJObj_data.getInt("id"), pJObj_data.getString("post_title"), pJObj_data.getString("post_content"));
                }

            } catch (JSONException e) {
                //Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                Log.d("Json","Exception = "+e.toString());
            }

            // checks if context is not null before updating posts page
            if (context != null){
                postsSect = (TextView) activity.findViewById(R.id.PostsSection);


                int lastFrJSONArray = postsHelper.getPostID().size() - 1;

                // outputs the id of the very first post, something to put to the textview
                postsSect.setText("id: " + postsHelper.getPostID().get(lastFrJSONArray - 2) + "\n");
                for (int i = lastFrJSONArray; i >= 0; i--)
                {
                    // appending the titles and contents of the current post
                    postsSect.append("title: " + postsHelper.getPostTitle().get(i) + "\n");
                    postsSect.append("content: " + postsHelper.getPostContent().get(i) + "\n");

                    // if this is the last post, then don't need to append id for the next post.
                    if (i != 0) {
                        postsSect.append("id: " + postsHelper.getPostID().get(i) + "\n");
                    }
                }
            }

        }

    }

UPDATE:

I have edited my JSON parsing algorithm.

Instead of parsing my JSON object from result as an array, this code here now parses it as an object. The JSONarray algorithm should be commented out.

The response you get from your webservice is actually not a JSONArray , but just a simple JSONObject . Hence change this line:

JSONArray pJObjArray = new JSONArray(result);

to

JSONObject pJObjArray = new JSONObject(result);

If You are not getting token just follow this code. it may help you.

    //member variable
    SharedPreferences ShPreference;
    SharedPreferences.Editor PrefEditor;
    String ApiToken;

    OnCreate(){

   ShPreference = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    ApiToken =  ShPreference.getString(accessToken, "");

    //  call wherever you want
    new SendPostsRequest(ApiToken).execute()

    }



  public class SendPostsRequest extends AsyncTask<String, String, String> {
    private String APIToken;
    TextView postsSect;
    // Add a pre-execute thing
    HttpURLConnection urlConnection;

    // gets the activity context


    // constructor
    public SendPostsRequest(String APIToken){
        this.APIToken = APIToken;
    }

    @Override
    protected String doInBackground(String... params) {

        StringBuilder result = new StringBuilder();

        // retrieves the context passed
        Context context = mPostReference.get();

        String APIAuthentication = APIToken; // or you can direct pass

        try {
            // Sets up connection to the URL (params[0] from .execute in "login")
            urlConnection = (HttpURLConnection) new URL(params[0]).openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty ("Authorization", APIAuthentication);
            urlConnection.connect();

            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

        }catch( Exception e) {
            e.printStackTrace();
        }
        finally {
            urlConnection.disconnect();
        }

        return result.toString();
    }

    @Override
    protected void onPostExecute(String result) {

        // expecting a response code fro my server upon receiving the POST data
        Log.e("TAG", result);

        // gets the JSON files stored in the posts details class from Posts Activity
        Posts.PostsDetails postsHelper = new Posts().new PostsDetails();

        // retrieves the context passed
        Context context = mPostReference.get();

        // For posts
        try {
            JSONArray pJObjArray = new JSONArray(result);

            // algorithm for parsing the JSONArray from the Django REST API
            for (int i = 0; i < pJObjArray.length(); i++) {
                // puts the current iterated JSON object from the array to another temporary object
                JSONObject pJObj_data = pJObjArray.getJSONObject(i);
                // inputs necesarry elements to the ListPosts function
                postsHelper.setPost(pJObj_data.getInt("id"), pJObj_data.getString("post_title"), pJObj_data.getString("post_content"));
            }

        } catch (JSONException e) {
            //Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
            Log.d("Json","Exception = "+e.toString());
        }

        // checks if context is not null before updating posts page
        if (context != null){
            postsSect = (TextView) activity.findViewById(R.id.PostsSection);


            int lastFrJSONArray = postsHelper.getPostID().size() - 1;

            // outputs the id of the very first post, something to put to the textview
            postsSect.setText("id: " + postsHelper.getPostID().get(lastFrJSONArray - 2) + "\n");
            for (int i = lastFrJSONArray; i >= 0; i--)
            {
                // appending the titles and contents of the current post
                postsSect.append("title: " + postsHelper.getPostTitle().get(i) + "\n");
                postsSect.append("content: " + postsHelper.getPostContent().get(i) + "\n");

                // if this is the last post, then don't need to append id for the next post.
                if (i != 0) {
                    postsSect.append("id: " + postsHelper.getPostID().get(i) + "\n");
                }
            }
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM