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)。任意のヘルプありがとうございました。
これはタイプミスですか、 'username'と' password'の両方に同じキーですか? –
同様の質問がありました[ここ](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) 。 – Makk0
しかし、私は結果を取るしたいと思います。これはmysqlのjsonです。パスワードとユーザー名ではありません。 jsonには、名前、性別、年齢などの情報があります。私はこのjsonを取り、構文解析し、textViewsにこの情報を入力したいと考えています。 – adamkocalek