Java 如何使用截击登录?
我是Java(android studio)新手 我尝试使用截击来完成Post http请求 我的目标是登录 我做了一些研究,发现发送表单数据的方法是这样的 但是我迷路了 这是我的全部代码 build.gradle(模块:应用程序) AndroidManifest.xmlJava 如何使用截击登录?,java,android-volley,Java,Android Volley,我是Java(android studio)新手 我尝试使用截击来完成Post http请求 我的目标是登录 我做了一些研究,发现发送表单数据的方法是这样的 但是我迷路了 这是我的全部代码 build.gradle(模块:应用程序) AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.homesweethome">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您需要使用此字符串请求方法将登录请求发布到需要将参数映射到请求的位置。
下面显示的示例代码带有某些注释,您需要在其中进行更改。希望能有所帮助。
StringRequest request = new StringRequest(Request.Method.POST, //your url here// , response -> {
if(response != null){
/////do something here/////
} catch (JSONException e) {
e.printStackTrace();
}
}
}, error -> {
error.printStackTrace();
}) {
//// pass parameters id and password here////
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("email", id.getText().toString().trim());
map.put("password", pass.getText().toString().trim());
return map;
}
};
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(request);
StringRequest-request=new-StringRequest(request.Method.POST,//您的url在这里//,response->{
if(响应!=null){
/////在这里做点什么/////
}捕获(JSONException e){
e、 printStackTrace();
}
}
},错误->{
错误。printStackTrace();
}) {
////在此传递参数id和密码////
@凌驾
受保护的映射getParams()引发AuthFailureError{
HashMap=newHashMap();
map.put(“email”,id.getText().toString().trim());
map.put(“password”,pass.getText().toString().trim());
返回图;
}
};
RequestQueue=Volley.newRequestQueue(LoginActivity.this);
添加(请求);
看看我下面的源代码
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response",""+response);
//If we are getting success from server
if (response.equals("success")) {
//Creating a shared preference
SharedPreferences sp = LoginActivity.this.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sp.edit();
//Adding values to editor
editor.putString(Constant.ROLL_SHARED_PREF, roll);
//Saving values to editor
editor.apply();
//Starting Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
}
else if(response.equals("failure")) {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Roll or Password is not valid", Toast.LENGTH_LONG).show();
}
else {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Invalid user cell or password", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//You can handle error here if you want
Toast.makeText(LoginActivity.this, "There is an error !!!", Toast.LENGTH_LONG).show();
loading.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//Adding parameters to request
params.put(Constant.KEY_ROLL, roll);
params.put(Constant.KEY_PASSWORD, password);
//returning parameter
return params;
}
};
//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
//创建字符串请求
StringRequest StringRequest=新的StringRequest(Request.Method.POST、Constant.LOGIN\u URL、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(“响应”,“响应+响应”);
//如果我们从服务器获得成功
if(response.equals(“success”)){
//创建共享首选项
SharedReferences sp=LoginActivity.this.getSharedReferences(Constant.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//创建编辑器以将值存储到共享首选项
SharedReferences.Editor=sp.edit();
//向编辑器添加值
editor.putString(Constant.ROLL\u SHARED\u PREF,ROLL);
//将值保存到编辑器
editor.apply();
//开始家庭活动
意向意向=新意向(LoginActivity.this、HomeActivity.class);
星触觉(意向);
Toast.makeText(LoginActivity.this,“登录成功”,Toast.LENGTH_SHORT.show();
}
else if(响应等于(“失败”)){
//如果服务器响应不成功
//在toast上显示错误消息
Toast.makeText(LoginActivity.this,“滚动或密码无效”,Toast.LENGTH_LONG.show();
}
否则{
//如果服务器响应不成功
//在toast上显示错误消息
Toast.makeText(LoginActivity.this,“无效用户单元格或密码”,Toast.LENGTH_LONG.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//如果需要,可以在此处处理错误
Toast.makeText(LoginActivity.this,“有错误!!!”,Toast.LENGTH\u LONG.show();
loading.dispose();
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
//向请求添加参数
参数放置(恒定键滚动,滚动);
参数put(常量密钥密码、密码);
//返回参数
返回参数;
}
};
//将字符串请求添加到队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
我从我的git存储库中获取了此源代码
也许如果你访问以下链接,你会更正确地理解它
请在回答中添加解释。在stackoverflow中,只使用代码的答案是非常不受欢迎的。好的,我会记住这一点@ariefbayu@EmonHossainMunna,我在您突出显示MainActivity.java代码时对其进行了修改。但我仍然无法记录响应。我为什么做错了?您可以查看上面的my MainActivity.java。@SafiahNadiah当您从登录活动点击登录按钮时,将调用此方法。这意味着您需要在LoginButton.setOnClickListener方法下调用此方法。
StringRequest request = new StringRequest(Request.Method.POST, //your url here// , response -> {
if(response != null){
/////do something here/////
} catch (JSONException e) {
e.printStackTrace();
}
}
}, error -> {
error.printStackTrace();
}) {
//// pass parameters id and password here////
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("email", id.getText().toString().trim());
map.put("password", pass.getText().toString().trim());
return map;
}
};
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(request);
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response",""+response);
//If we are getting success from server
if (response.equals("success")) {
//Creating a shared preference
SharedPreferences sp = LoginActivity.this.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sp.edit();
//Adding values to editor
editor.putString(Constant.ROLL_SHARED_PREF, roll);
//Saving values to editor
editor.apply();
//Starting Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
}
else if(response.equals("failure")) {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Roll or Password is not valid", Toast.LENGTH_LONG).show();
}
else {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Invalid user cell or password", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//You can handle error here if you want
Toast.makeText(LoginActivity.this, "There is an error !!!", Toast.LENGTH_LONG).show();
loading.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//Adding parameters to request
params.put(Constant.KEY_ROLL, roll);
params.put(Constant.KEY_PASSWORD, password);
//returning parameter
return params;
}
};
//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}