【问题标题】:Issue with Asynctask in Android MySQL server request classAndroid MySQL 服务器请求类中的 Asynctask 问题
【发布时间】: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


【解决方案1】:

这是因为你试图返回一个字符串,但是你在方法头中指定的返回类型是Void

因此,将其更改为 String 将解决您的错误。

一般来说:从安全性的角度来看,您不应通过不安全的连接(例如 http)将凭据作为密码发送。在 android 中创建更安全的 https 连接非常容易。如果您想了解更多相关信息,请发表评论。

【讨论】:

  • 没有解决问题,只是产生了额外的错误。顺便说一句,遵循了一个教程,它对那个人有用。
  • @BillHicks 可以添加链接吗?
  • 正如我告诉你的,他还在 16:27 分钟将方法的返回类型更改为 Stringyoutu.be/cOsZHuu8Qog?t=16m27s
  • 哇,完全错过了,我觉得很愚蠢,无论如何,谢谢,我很感激。
  • @BillHicks 没问题,我很高兴能为您提供帮助。每个人都会错过一些东西。请记住接受对您有帮助的答案,以便其他遇到您问题的用户可以看到解决了您的问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
相关资源
最近更新 更多