-1

I have a problem with passing data(values) from java class to activity and display it in textView. Sharedpreferences is not working. This is fragment from my class:

else if (type.equals("getUser")) {
            try {
                String user_name = params[1];
//                String password = params[2];
                URL url = new URL(getUser_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
//                String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
//                        + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                System.out.println(result);
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
@Override
    protected void onPostExecute(String result) {
        String temp = "Login success. Welcome!";

        if (result.equals(temp)) {
            Intent intent = new Intent(context, ProjectsActivity.class);
            context.startActivity(intent);
        } else if (result.contains("[{")) {

        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }

and I want to pass my result to textview in activity. This activity (UserAreaActivity).

public class UserAreaActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);


        TextView textViewUserArea = (TextView) findViewById(R.id.textViewUserArea);
        EditText editTextName = (EditText) findViewById(R.id.editTextName);
        EditText editTextSurname = (EditText) findViewById(R.id.editTextrSurname);
        EditText editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        EditText editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        EditText editTextAge = (EditText) findViewById(R.id.editTextAge);

//
        String type = "getUser";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username);
    }

Thanks a lot for any help.

  • Is it a typo, Same key for both `username` and `password`? – K Neeraj Lal Nov 15 '16 at 16:15
  • A similar question got asked [here](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a). – Makk0 Nov 15 '16 at 16:19
  • But i wanna take a result. It is json from mysql. Not password and username. In json there are info like name, surrname, age etc. I want to take this json and parse, then fill textViews with this informations. – adamkocalek Nov 15 '16 at 19:33

1 Answers1

1
 SharedPreferences myprefs = getSharedPreferences("user", MODE_WORLD_READABLE);
    String username = myprefs.getString("username", null);
    String password = myprefs.getString("password", null); //Typo, you had used "username" here

Your are not saving your data in sharedpreferences before calling this, hence you get null for both username and password and you don't see anything.

You need to save your data first, before you can access them anytime.

 //Code for writing into shared preferences

  SharedPreferences myprefs = getSharedPreferences("user", MODE_WORLD_READABLE);
   myprefs.edit().putString("username","Name to be saved").commit();
   myprefs.edit().putString("password","Password to be saved").commit();

SharedPreferences is used to save data between different app executions (When you close and open your app, you will be able to access data, provided you save the data sometime first).

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • But i wanna take a result. It is json from mysql. Not password and username. In json there are info like name, surrname, age etc. I want to take this json and parse, then fill textViews with this informations. – adamkocalek Nov 15 '16 at 18:51
  • In that case you would not need sharedpreferences at all...since you have a backend DB to retain the information. You need to read from your DB – SoulRayder Nov 16 '16 at 05:45