主要活动声明变量类型,用户名和密码
UsernameEt = (EditText)findViewById(R.id.et_username);
PasswordEt = (EditText)findViewById(R.id.et_password);
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
然后BackgroundWorker.java将通过声明params [value]来执行并使用值类型,用户名和密码......
@Override
protected Void doInBackground(String[] voids) {
String type = this.params[0];
String login_url = "http://10.0.2.2/ITSP/login.php";
if(type.equals("login")){
try {
String username = this.params[1];
String password = this.params[2];
URL url = new URL(login_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("")
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
但为什么“params [0]”在此代码中未定义?我正在使用android studio 3.1.3版。我应该更换params吗?我该怎么用?
答案 0 :(得分:0)
尝试这种方式获得params值
@Override
protected Void doInBackground(String[] voids) {
String type = voids[0];
String login_url = "http://10.0.2.2/ITSP/login.php";
if(type.equals("login")){
try {
String username = voids[1];
String password =voids[2];
URL url = new URL(login_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("")
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;