此代码用于GET请求。
public class Result扩展AppCompatActivity { 字符串ID; 登录login = new登录(); final String url =“http://192.168.1.189:9001/login”; // List login = new ArrayList<>(); 按钮注销; TextView nameTv,ageTv,emailTv,usernameTv; String NAME,AGE,EMAIL,USERNAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Intent intent=getIntent();
// ID=intent.getStringExtra("ID");
// new HttpGetTask().execute();
logout = (Button) findViewById(R.id.buttonLogout);
nameTv = (TextView) findViewById(R.id.nameTextView);
ageTv = (TextView) findViewById(R.id.ageTextView);
emailTv = (TextView) findViewById(R.id.emailTextView);
usernameTv = (TextView) findViewById(R.id.UsernameTextView);
nameTv.setText(NAME);
ageTv.setText(AGE);
emailTv.setText(EMAIL);
usernameTv.setText(USERNAME);
new HttpRequestTask().execute();
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent logoutintent = new Intent(Result.this, MainActivity.class);
startActivity(logoutintent);
}
});
}
private class HttpRequestTask extends AsyncTask<Void, Void, Login> {
@Override
protected Login doInBackground(Void... params) {
try {
// final String url = "http://192.168.1.213:9001/consumer/local/64";
RestTemplate restTemplate = new RestTemplate();
Login lg = restTemplate.getForObject(url, Login.class);
// NAME=lg.getName().toString();
Log.d("", "doInBackground:++++++++++++++++++++++ "+NAME);
return lg;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return null;
}
以下代码适用于POST请求。
public class Register扩展AppCompatActivity { 按钮按钮注册; public String ID; TextView editId,editName,editAge,editEmail,editUsername,editPassword; 私人字符串名称,年龄,电子邮件,用户名,密码; 登录login = new登录();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
editId=(TextView)findViewById(R.id.editId);
editName=(TextView)findViewById(R.id.editName);
editAge=(TextView)findViewById(R.id.editAge);
editEmail=(TextView)findViewById(R.id.editEmail);
editUsername=(TextView)findViewById(R.id.editUsername);
editPassword=(TextView)findViewById(R.id.editPassword);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Register.this, "Registration Successful", Toast.LENGTH_SHORT).show();
Name = editName.getText().toString();
Age = editAge.getText().toString();
Email = editEmail.getText().toString();
Username = editUsername.getText().toString();
Password = editPassword.getText().toString();
Log.d("???????????", "onClick:------------> " + Name);
Log.d("???????????", "onClick:------------> " + ID);
Log.d("???????????", "onClick:------------> " + Age);
Log.d("???????????", "onClick:------------> " + Email);
Log.d("???????????", "onClick:------------> " + Username);
Log.d("???????????", "onClick:------------> " + Password);
new HttpPostTask().execute();
Intent regintent = new Intent(Register.this, Result.class);
//regintent.putExtra("ID",1);
startActivity(regintent);
/*Intent regintent = new Intent(Register.this, Result.class);
startActivity(regintent);*/
}
});
}
public class HttpPostTask extends AsyncTask<Void,Void,Login>{
@Override
protected Login doInBackground(Void... params) {
final String url = "http://192.168.1.189:9001/login";
RestTemplate restTemplate = new RestTemplate();
Login login = new Login();
login.setName(Name);
login.setAge(Age);
login.setEMail(Email);
login.setUserName(Username);
login.getId();
login.setPassword(Password);
Log.d("???????????", "onClick:!!!!!!!!!!!!!!> " + Name);
login = restTemplate.postForObject(url, login, Login.class);
ID=login.getId();
Log.d("???????????", "onClick:!!!!!!!!!!!!!!> " + login.getId());
return login;
}
@Override
protected void onPostExecute(Login login){
super.onPostExecute(login);
}
}
}
答案 0 :(得分:1)