简体   繁体   English

未保存SharedPreferences

[英]SharedPreferences not getting saved

So I have been trying to figure out a way to save the Access Token I get from my API. 因此,我一直在尝试寻找一种方法来保存从API中获得的访问令牌。 I can successfully get the JSON response from my API and store it in my result variable within my doInBackground . 我可以从API成功获取JSON响应,并将其存储在doInBackground result变量中。

However, for some reason it is not getting saved in SharedPreferences in my onPostExecute . 但是,由于某种原因,它没有保存在我的onPostExecute中的SharedPreferences中。

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. result变量包含此JSON字符串{"access_token":"4Oq6o8oAGRf4oflu3hrbsy18qeIfG1","expires_in":36000,"token_type":"Bearer","scope":"read write","refresh_token":"iocSNJ2PTVbph2RnWmcf0Zv69PDKjw"}从我的API收到。

I have an algorithm that is supposed to save only the access_token for now. 我有一种算法,现在应该只保存access_token

My code is below: 我的代码如下:

WSAdapter.java 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. 下面的代码包含读取SharedPreferences的代码。 Which should be in the doInBackground of this AsyncTask as I need to put the access_token to the header. 这应该在此AsyncTask的doInBackground ,因为我需要将access_token放在标头中。

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. 我已经编辑了JSON解析算法。

Instead of parsing my JSON object from result as an array, this code here now parses it as an object. 现在,此代码不再将来自result JSON对象解析为数组,而是将其解析为对象。 The JSONarray algorithm should be commented out. JSONarray算法应被注释掉。

The response you get from your webservice is actually not a JSONArray , but just a simple JSONObject . 您从Web服务获得的响应实际上不是JSONArray ,而仅仅是一个简单的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");
                }
            }
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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