简体   繁体   English

Android Volley请求不将POST数据发送到服务器/数据库

[英]Android Volley Request not sending POST data to server/database

I am developing an android app that I have been trying to incorporate a social media login, I am trying to add the details retrieved via the Social media login (name & email) and add it to a database of users on our server via a Volley request. 我正在开发一个Android应用程序,我一直在尝试合并社交媒体登录,我试图添加通过社交媒体登录(名称和电子邮件)检索的详细信息,并通过排球将其添加到我们的服务器上的用户数据库请求。 So far I have managed to get the users details succesfully and add it to the Shared Preferences using the 'session' class I created, however calling another method to send the data to the server does not work. 到目前为止,我已经设法成功获取用户详细信息并使用我创建的“会话”类将其添加到共享首选项,但是调用另一种方法将数据发送到服务器不起作用。

facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        String fbID = null;
        String fbName = null;
        String fbEmail = null;
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            try {
                                fbID = object.getString("id");
                                fbName = object.getString("name");
                                fbEmail = object.getString("email");

                                session.setLogin(true);
                                session.setUserDetails(fbEmail,fbName);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            } finally {
                                registerSocialUser(fbName, fbEmail);
                            }
                        }
                    });

As you can see in my try/catch I make a request to the registerSocialUser() method via a 'finally' block. 正如您在我的try / catch中看到的,我通过'finally'块向registerSocialUser()方法发出请求。 This calls the following method fine and the Console log's the users details, however it is never added to the database. 这会调用以下方法,控制台日志是用户详细信息,但它永远不会添加到数据库中。

private void registerSocialUser (final String name, final String email) {
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.POST, Config.REGISTER_SOCIAL_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("CREATION", "Social Media Account Added: " + name + " " + email);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("CREATION", "Create Error");
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("user",name); //name
            params.put("email",email); //email
            return params; //return the parameters

        }
    };
    queue.add(request);
    Log.d("CREATION", "SUCCESS");
}

The logcat returns: logcat返回:

Social Media Account Added: *My name* *My email*

The Config.REGISTER_SOCIAL_URL is a php file which takes two POST parameters (name and email) and adds them to the database after checking it doesn't already exist. Config.REGISTER_SOCIAL_URL是一个php文件,它接受两个POST参数(名称和电子邮件),并在检查它尚不存在后将它们添加到数据库中。 I can call this manually through my browser with exactly the same details and it works fine and adds to the database. 我可以通过我的浏览器手动调用它具有完全相同的细节,它工作正常,并添加到数据库。

Excuse me if any of my code is really bad practice, i'm pretty new to Android Development so would really appreciate the help :) 对不起,如果我的任何代码都是非常糟糕的做法,我对Android开发很新,所以非常感谢帮助:)

While sending parameters you can easily use POST request method. 发送参数时,您可以轻松使用POST请求方法。 There are plenty of chances that you would have been missing some method. 有很多机会你会错过一些方法。

    private void registerSocialUser (final String name, final String email) {
RequestQueue queue = Volley.newRequestQueue(this);
String stopArchiveUrl = Config.REGISTER_SOCIAL_URL;
StringRequest stringRequest = new StringRequest(Request.Method.POST, stopArchiveUrl,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("CREATION", "Social Media Account Added: " + name + " " + email);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("ERROR", "Error adding Social Media Account");
        }
}){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("name",name); //name
            params.put("email",email); //email
            return params; //return the parameters
        }
    };
// Add the request to the RequestQueue.
queue.add(stringRequest);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();

} }

I think problem not lies in your code. 我认为问题不在于你的代码。 It is the strings that you are passing in the parameters while using POST, you are using 您正在使用的是使用POST时在参数中传递的字符串

                   params.put("user",name); //name

which should be 应该是

                    params.put("name",name); //name

Also it would be better if you attach a token using a secret key for validation on the server side too. 如果你在服务器端使用密钥附加一个令牌进行验证也会更好。 So, that no one hit your url (if leaked somehow) anonymously. 所以,没有人匿名打你的网址(如果泄露)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM