java. util. prefs. Preferences. Put() is valid, but preferences Nodeexists() always returns false

I have some simple code for learning OAuth authentication using the landmark library I'm trying to use Java util. prefs. Preferences saves the request token, token key, and pin as Java preferences Putting data into preferences works well (I check that the file has been created and store information) However, after I put the data into the preferences file, I immediately try to check whether the node exists and always get an error

I also tried to rerun the code and check the existence of the node before trying to save the data again, but I still got an error

What did I do wrong?

This is the code:

package com.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.prefs.Preferences;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;

public class ReadMain {

    public static void main(String[] args) throws Exception {

        OAuthConsumer consumer = 
        new DefaultOAuthConsumer(
                "??","??");

        OAuthProvider provider = 
        new DefaultOAuthProvider(
            "https://www.readability.com/api/rest/v1/oauth/request_token/","https://www.readability.com/api/rest/v1/oauth/access_token/","https://www.readability.com/api/rest/v1/oauth/authorize/");

        System.out.println("Fetching request token from Readability...");

        Preferences prefs = Preferences.userNodeForPackage(ReadMain.class);

        final String PREF_TokenSecret    = "com/example/TS";
        final String PREF_RequestToken   = "com/example/RT";
        final String PREF_ReadabilityPin = "com/example/RP";

        boolean exists = 
            prefs.nodeExists( PREF_TokenSecret ) &&
        prefs.nodeExists( PREF_RequestToken ) &&
        prefs.nodeExists( PREF_ReadabilityPin );

        if ( prefs.nodeExists( PREF_TokenSecret ) )
        {
        System.out.println("Token secret exists!");
        }

        if ( prefs.nodeExists( PREF_RequestToken ) )
        {
        System.out.println("Request token exists!");
        }

        if ( prefs.nodeExists( PREF_ReadabilityPin ) )
        {
        System.out.println("Readability pin exists!");
        }

        String pin;

        if ( exists )
        {
        consumer.setTokenWithSecret( 
                prefs.get(PREF_RequestToken,""),prefs.get(PREF_TokenSecret,"") );
            pin = prefs.get(PREF_ReadabilityPin,"");
        }
        else
        {
        // we do not support callbacks,thus pass OOB
        String authUrl = provider.retrieveRequestToken(consumer,OAuth.OUT_OF_BAND);

        System.out.println( "Request token: " + consumer.getToken() );
        System.out.println( "Token secret: " + consumer.getTokenSecret() );

        prefs.put( PREF_RequestToken,consumer.getToken() );
        prefs.put( PREF_TokenSecret,consumer.getTokenSecret() );

        System.out.println( "Now visit:\n" + authUrl + "\n... and grant this app authorization" );
        System.out.println( "Enter the PIN code and hit ENTER when you're done:" );

        BufferedReader br = new BufferedReader(new InputStreamReader(system.in));
        pin = br.readLine();

        prefs.put(PREF_ReadabilityPin,pin);

        if ( prefs.nodeExists( PREF_TokenSecret ) )
        {
            System.out.println("Token secret exists!");
        }

        if ( prefs.nodeExists( PREF_RequestToken ) )
        {
            System.out.println("Request token exists!");
        }

        if ( prefs.nodeExists( PREF_ReadabilityPin ) )
        {
            System.out.println("Readability pin exists!");
        }
        }

        System.out.println("Fetching access token from Readability...");
        provider.retrieveAccessToken(consumer,pin);

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("https://www.readability.com/api/rest/v1/bookmarks?user=marcusps&archive=1");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request to Readability...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
            + request.getResponseMessage());
    }
}

Solution

put(..) The opposite operation is get (..) So:

boolean exists = prefs.get(key,null) != null;

If nothing is found under the specified key, get (..) Method returns the second parameter

Nodeexists() is related to the hierarchy of preferences and can be used in prefs node(..) Used when creating nodes But you seem to need a flat structure, so I don't think you need a hierarchy

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>