java - 字符串请求发布未显示任何错误
问题描述
我正在使用字符串请求在 Android Studio 中发出发布请求,当我调试时,我没有收到任何错误。调试时,我没有在代码中获取 JSON 对象。它跳过登录请求并结束调试。如果我没有做正确的事情。请尝试纠正它
这是 JSON 对象
{"RESPONSECODE":200,
"RESPONSEDATA:[{"id_User":"120","FirstName":"King",
"LastName":"Dosty","Role_Id":"2","Email":"donmister5000@gmail.com","location":null,"Password":"$2y$10$fJJH6qOuhhXaDadHQhZefemBwHPZ3aHid\/WF579DwVJo8XyVGaEN6",
}],"Success":true}
这是 loginRequest java 类
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://localhost/project/index.php/clientapinew/post_login2";
private Map<String, String> params;
public LoginRequest(String Email,String Password, Response.Listener<String> listener){
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("Email", Email);
params.put("Password", Password);
}
@Override
public Map<String, String> getParams(){
return params;
}
}
这是在活动中单击发送请求的登录按钮
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Email = emailEdt.getText().toString();
String Password = passwordEdt.getText().toString();
LoginRequest loginRequest = new LoginRequest(Email, Password,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
Log.d(TAG, jsonResponse.getString("SUCCESS"));
boolean success = jsonResponse.getBoolean("SUCCESS");
if (success)
{
Intent intent = new Intent (LoginActivity.this,MainActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();}
else {
AlertDialog.Builder builder = new
AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed").setNegativeButton("Retry", null)
.create().show();
}
}
catch (JSONException e)
{ e.printStackTrace();}}
});
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
这是我调试时的 url get 和 params
[] localhost/project/index.php/clientapinew/post_login2 0x59c3b57d NORMAL null 电子邮件:john@gmail.com 密码:azerty
解决方案
我建议你放弃 LoginRequest 类,并在你的 LoginActivity 中添加这个方法:
private void login(final String email, final String password){
String LOGIN_REQUEST_URL = "http://localhost/project/index.php/clientapinew/post_login2";
// JSON data
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("Email", email);
jsonObject.put("Password", password);
} catch (JSONException e){
e.printStackTrace();
}
// Json request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
LOGIN_REQUEST_URL,
jsonObject,
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response){
//Toast.makeText(context, "Product successfully added", Toast.LENGTH_SHORT).show();
try{
//use the response JSONObject now like this log
Log.d(TAG, response.getString("Success"));
boolean success = response.getBoolean("Success");
if (success) {
//...
}
} catch (JSONException e) {
System.out.println("Error logging in");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
Toast.makeText(LoginActivity.this, "Can't connect to Internet. Please check your connection.", Toast.LENGTH_LONG).show();
}
else if (error instanceof ServerError) {
Toast.makeText(LoginActivity.this, "Unable to login. Either the username or password is incorrect.", Toast.LENGTH_LONG).show();
}
else if (error instanceof ParseError) {
Toast.makeText(LoginActivity.this, "Parsing error. Please try again.", Toast.LENGTH_LONG).show();
}
else if (error instanceof NoConnectionError) {
Toast.makeText(LoginActivity.this, "Can't connect to internet. Please check your connection.", Toast.LENGTH_LONG).show();
}
else if (error instanceof TimeoutError) {
Toast.makeText(LoginActivity.this, "Connection timed out. Please check your internet connection.", Toast.LENGTH_LONG).show();
}
//Do other stuff if you want
error.printStackTrace();
}
}){
@Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String,String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
3600,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueueSingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
然后你的 onClick 应该看起来像
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Email = emailEdt.getText().toString();
String Password = passwordEdt.getText().toString();
login(Email, Password);
}
}
创建 RequestQueueSingleton.java 类并使用如下内容:
public class RequestQueueSingleton {
private static RequestQueueSingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private RequestQueueSingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized RequestQueueSingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new RequestQueueSingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}
推荐阅读
- javascript - TypeError:text.select 不是函数
- r - 在r中将数据从一个数据帧提取到另一个数据帧
- php - 在订单管理页面 WooCommerce 中编辑元数据
- image - Xamarin:将下载的图像从页面传递到另一个页面
- google-cloud-platform - 由于有关 Cloud SQL 联合连接不在“查询区域”中的 BigQuery 警告消息,GCP 数据流失败
- elasticsearch - Logstash - 如果 %{statement} 有“价值”
- javascript - Cloud Functions Firestore 触发器 - “无法读取未定义的属性‘eventType’”
- css - 物化 CSS - 让徽章左对齐?
- haskell - XMonad“切换”键绑定
- python - 使用函数参数过滤 CSV 文件