2016-11-15 62 views
-1

我有一个从java类传递数据(值)到活动并显示在textView中的问题。 Sharedpreferences不起作用。 这是我的课的片段:Android如何将数据从课程传递到活动?

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(); 
     } 
    } 

,我想通过我的结果在活动的TextView。此活动(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); 
    } 

非常感谢您的帮助。

+0

这是一个错字,“用户名”和“密码”都一样吗? –

+0

一个类似的问题被问到[这里](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) 。 – Makk0

+0

但我想要一个结果。这是从MySQL的JSON。不是密码和用户名。在json中有像名字,姓氏,年龄等信息。我想采取这个JSON和解析,然后用这些信息填充textViews。 – adamkocalek

回答

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 

你不调用此之前节省sharedpreferences您的数据,因此你收到null两个用户名和密码,你看不到任何东西。

您需要先保存数据,然后才能随时访问它们。

​​

SharedPreferences用来保存不同的应用程序执行之间的数据(当你关闭和打开您的应用程序,您将能够访问数据,只要你保存数据的某个时候第一个)。

+0

但我想要一个结果。这是从MySQL的JSON。不是密码和用户名。在json中有像名字,姓氏,年龄等信息。我想采取这个JSON和解析,然后用这些信息填充textViews。 – adamkocalek

+0

在这种情况下,您根本不需要sharedpreferences ...因为您有一个后端数据库来保留这些信息。您需要从您的数据库中读取数据 – SoulRayder

相关问题