2016-11-15 7 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)。任意のヘルプありがとうございました。

+0

これはタイプミスですか、 'username'と' password'の両方に同じキーですか? –

+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 

あなたは、あなたがユーザ名とパスワードの両方にnullを取得し、あなたが何も表示されないため、これを呼び出す前sharedpreferencesでデータを保存されていません。

いつでもデータにアクセスするには、まずデータを保存する必要があります。

​​

SharedPreferencesが異なるアプリの実行の間でデータを保存するために使用されている(あなたが近く、アプリを開くと、あなたはいつか最初にデータを保存してデータにアクセスすることができるようになります)。

+0

しかし、私は結果を取ってみたい。これはmysqlのjsonです。パスワードとユーザー名ではありません。 jsonには、名前、性別、年齢などの情報があります。私はこのjsonを取り、構文解析し、textViewsにこの情報を入力したいと考えています。 – adamkocalek

+0

その場合、情報を保持するバックエンドDBがあるので、sharedpreferencesはまったく必要ありません。あなたはあなたのDBから読む必要があります – SoulRayder

関連する問題