我正在开发一个Android应用程序,尝试整合社交媒体登录名,我试图添加通过社交媒体登录名(姓名和电子邮件)检索的详细信息,并通过Volley将其添加到我们服务器上的用户数据库中请求。
到目前为止,我已经成功地成功获取了用户的详细信息,并使用我创建的“会话”类将其添加到“共享首选项”中,但是调用另一种方法将数据发送到服务器是行不通的。
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);
}
}
});
如您在我的try / catch中所看到的,我通过“ finally”块向registerSocialUser()方法发出了请求。这样可以很好地调用以下方法,并在控制台日志中记录用户详细信息,但是永远不会将其添加到数据库中。
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");
}
logcat返回:
Social Media Account Added: *My name* *My email*
Config.REGISTER_SOCIAL_URL是一个php文件,它带有两个POST参数(名称和电子邮件),并在检查它不存在后将它们添加到数据库中。我可以使用完全相同的详细信息通过浏览器手动调用它,并且可以正常工作并将其添加到数据库中。
对不起,如果我的代码中的任何一项都不是很好的做法,那么我对Android开发来说还是很新的,因此非常感谢您的帮助:)
最佳答案
发送参数时,您可以轻松使用POST请求方法。您很可能会错过某些方法。
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();
}
我认为问题不在于您的代码。它是您在使用POST时正在传递参数的字符串,您正在使用
params.put("user",name); //name
应该是
params.put("name",name); //name
同样,如果您也使用密钥在服务器端进行验证附加令牌,那将更好。因此,没有人会匿名点击您的网址(如果以某种方式泄露)。