0
votes

hi all i've got this code to save some datas in sharedprefs but android studio tells me "getDefaultSharedPrefs in Preference Manager cannot be applied to com.foo.downloadDB.AttemptLogin" how can i do to solve this error?

i seems like i can't save to my sharedprefs from a "child" class here is the code:

public class downloadDB extends Activity  {

    private EditText user, pass;
    private android.widget.Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;
    //JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php login script location:

    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://www.myurl.com/users.php";

    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";

    //JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";



    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_db);
        new AttemptLogin().execute();


    }


   public class AttemptLogin extends AsyncTask<String, String, String>  {

        boolean failure = false;
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(downloadDB.this);
            pDialog.setMessage("Download..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

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


            //Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();


            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(
                    LOGIN_URL, "POST", params);

            //check your log for json response
            Log.d("Login attempt", json.toString());

            editor1.putString("JSON_DB", json.toString());
            editor1.commit();
            System.out.println(editor1.commit());

            return null;
        }

        // After completing background task Dismiss the progress dialog

        protected void onPostExecute(String file_url) {
            //dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(downloadDB.this, file_url, Toast.LENGTH_LONG).show();
            }

            // Intent intent = new Intent(downloadDB.this, SampleActivity.class);
            // startActivity(intent);
        }
    }
}

EDIT

I have this method in my SampleActivity class

private void LoadPreferences(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String  name = sharedPreferences.getString("name", "0") ;
    String  email = sharedPreferences.getString("email", "0") ;
    String  id = sharedPreferences.getString("id", "0") ;
    String  Database = sharedPreferences.getString("JSON_DB", "0") ;
    Toast.makeText(this, Database, Toast.LENGTH_LONG).show();
    //Toast.makeText(this, id, Toast.LENGTH_LONG).show();
    //Toast.makeText(this, email, Toast.LENGTH_LONG).show();
}

and the Toast works fine, it prints me the JSON_DB correctly, but when i try to reach it from the respective Fragment it doesn't work.. here is my fragment code :

public class SampleFragment extends Fragment {


    public static String KEY_ID = "id";
    public static String KEY_EMAIL = "email";
    public static String KEY_NAME = "name";
    public static String JSON_DB = "";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        LoadPreferences();

        Log.i("string", KEY_EMAIL);
        Log.i("string", KEY_ID);
        Log.i("string", KEY_NAME);
        Log.i("string", JSON_DB);

and KEY_EMAIL, KEY_ID and other objects logs correctly..

1

1 Answers

0
votes

This is the problem:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

this is the instance of the AsyncTask instead Activity

use downloadDB.this instead only this; but please call your class using Uppercase (DownloadDB)