【发布时间】:2015-12-20 21:15:03
【问题描述】:
我一直在关注这个关于通过 PHP 和 mysql 向 android 应用程序注册用户的教程,但我遇到了 AsyncTask 类的问题。代码如下:
public class ServerRequests extends AsyncTask<String, Void, String> {
Context context;
ServerRequests(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String REG_URL = "http://127.0.0.1/ibumobile/register.php";
String LOGIN_URL = "http://127.0.0.1/ibumobile/login.php";
String method = params[0];
if(method.equals("register")) {
String email = params[1];
String studentID = params[2];
String password = params[3];
try {
URL url = new URL(REG_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//Output to the server
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
URLEncoder.encode("studentID", "UTF-8") + "=" + URLEncoder.encode(studentID, "UTF-8") + "&" +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
//Input from the server
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Registration successful...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
错误指出doInBackground() 使用了不兼容的返回类型,我完全理解,但我无法修复它。
如果您可能需要,这里是注册活动代码:
public class Register extends Activity {
EditText email, studentID, password, repPassword;
String regEmail, regStudentID, regPassword, regRepPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find Text Form by id
email = (EditText)findViewById(R.id.regEmail);
studentID = (EditText)findViewById(R.id.regStudentID);
password = (EditText)findViewById(R.id.regPassword);
repPassword = (EditText)findViewById(R.id.regRepPassword);
//startActivity(new Intent(this, Login.class));
}
public void registerUser(View view) {
//Convert text form input to string
regEmail = email.getText().toString();
regStudentID = studentID.getText().toString();
regPassword = password.getText().toString();
regRepPassword = repPassword.getText().toString();
String method = "register";
ServerRequests serverRequests = new ServerRequests(this);
serverRequests.execute(method, regEmail, regStudentID, regPassword, regRepPassword);
finish();
}
}
谢谢。 :)
【问题讨论】:
-
@SebastianWalla 它实际上将这一行检测为问题:
return "Registration successful..."; -
请看看我的回答@Bill Hick's
标签: php android mysql android-asynctask