duanchandun1860 2016-06-17 23:36
浏览 87
已采纳

Android Studio App不会运行数据库

The app runs, but a particular function wont. Whenever I try to login or Register an account, The Gradle console says it skipped frames and there's too much running. What I am trying to do is take in user information and send it to a database. This is the register activity code that has the problem. If i take the JSON out and just have the new activity open, it works.

 Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        if (success) {
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(username, email, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}
}

Php code for the Register:

 $username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "INSERT INTO data (username, email, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $username, $email, $password);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

Register Request Code:

private Map<String, String> params;

public RegisterRequest(String username, String email, String password, Response.Listener<String> listener){
    /*
    NExt line means we are going to pass some information into the register.php
     */
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    /*
    This is how we pass in the information from the register to the thing, we are using a hashmap
     */
    params = new HashMap<>();
    params.put("username", username);
    params.put("email", email);
    params.put("password", password);

}
/*
Volley needs to get the data so we do a get params
Which gives us this method
 */

@Override
public Map<String, String> getParams() {
    return params;
}
}

Does anyone know how I can solve this??? I don't know how to input Async task in this and if anyone can, please help. Is there anyway to fix this without an Async task? Thank You!

  • 写回答

1条回答 默认 最新

  • dqxafj6830 2016-06-18 05:35
    关注

    You are probably running a network request on the main thread, that is the thread that is used by the Android Framework for rendering the UI. You need to have some kind of mechanism that does the networking tasks on another thread. AsyncTask is the simplest of them all to implement, and is good for your scenario, as it is a simple task.

    Extend AsyncTask and pass your request parameters to it in a Map:

    public class RegisterTask extends AsyncTask<Map, Void, Boolean> {
    
        @Override
        protected Boolean doInBackground(Map... params) {
            Map props = params[0];  // you can access your request params here
    
            /*
             Do your network request here, using HttpUrlConnection or 
             HttpClient. and return a result (boolean in this example),
             which is passed to the onPostExecute method
            */
            return false;
        }
    
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            // This method is run on the main thread, so you can
            // update your UI after the request is completed.
        }
    }
    

    You can execute this task like this:

    RegisterTask task = new RegisterTask();
    task.execute(yourHashMapContainingData);
    

    Check out this official Google documentation for more details: Perform Network Operations on a Separate Thread

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog