我有一个从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);
}
非常感谢您的帮助。
这是一个错字,“用户名”和“密码”都一样吗? –
一个类似的问题被问到[这里](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