Java 提交的字符串请求未显示任何错误

Java 提交的字符串请求未显示任何错误,java,android,json,android-volley,Java,Android,Json,Android Volley,我在androidstudio中使用stringrequest发出一个post请求,当我调试时,没有收到任何错误。调试时,代码中没有JSON对象。它跳过登录请求,调试结束。如果我做的操作不正确,请尝试更正 这是JSON对象 {"RESPONSECODE":200, "RESPONSEDATA:[{"id_User":"120","FirstName":"King", "LastName":"Dosty","Role_Id":"2","Email":"donmister5000@gmail.com

我在androidstudio中使用stringrequest发出一个post请求,当我调试时,没有收到任何错误。调试时,代码中没有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}
这是LoginRequestJava类

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;
}
}
公共类LoginRequest扩展了StringRequest{
私有静态最终字符串登录\u请求\u URL=”http://localhost/project/index.php/clientapinew/post_login2";
私有映射参数;
公共登录请求(字符串电子邮件、字符串密码、响应.侦听器){
super(Request.Method.POST,LOGIN\u Request\u URL,listener,null);
params=新的HashMap();
参数put(“电子邮件”,电子邮件);
参数put(“密码”,密码);
}
@凌驾
公共映射getParams(){
返回参数;
}
}
这是在活动中单击时发送请求的登录按钮

 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);

        }
    });
loginBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串Email=emailEdt.getText().toString();
字符串密码=passwordEdt.getText().toString();
LoginRequest LoginRequest=新登录请求(电子邮件、密码、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
Log.d(TAG,jsonResponse.getString(“SUCCESS”);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功)
{
意向意向=新意向(LoginActivity.this,MainActivity.class);
星触觉(意向);
Toast.makeText(LoginActivity.this,“登录成功”,
Toast.LENGTH_SHORT).show();}
否则{
AlertDialog.Builder=新建
AlertDialog.Builder(LoginActivity.this);
builder.setMessage(“登录失败”).setNegativeButton(“重试”,null)
.create().show();
}
} 
捕获(JSONException e)
{e.printStackTrace();}}
});
RequestQueue=Volley.newRequestQueue(LoginActivity.this);
添加(loginRequest);
}
});
这是调试时获取的url和参数

[]localhost/project/index.php/clientapinew/post_login2 0x59c3b57d正常为空 电邮:john@gmail.com 密码:azerty


响应中的第一个字符是“>”。当它尝试运行此行时:

JSONObject jsonResponse = new JSONObject(response);
它在响应中找不到JsonObject,您的代码将无法工作。
我的建议是从您的响应中删除“>”并重试。

我建议您放弃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);
    }
创建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);
}
}
公共类RequestQueueSingleton{
私有静态请求队列单实例;
私有请求队列mRequestQueue;
私有静态上下文mCtx;
私有RequestQueueSingleton(上下文){
mCtx=上下文;
mRequestQueue=getRequestQueue();
}
公共静态同步请求队列Singleton getInstance(上下文){
if(minInstance==null){
MinInstance=新的RequestQueueSingleton(上下文);
}
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
//getApplicationContext()是关键,它可以防止泄漏
//活动或广播接收器(如果有人传入)。
mRequestQueue=Volley.newRequestQueue(mCtx.getApplicationContext());
}
返回mrequest队列;
}
公共无效addToRequestQueue(请求请求){
getRequestQueue().add(请求);
}
}

它不起作用,在为LoginRequest.java扩展JsonObjectRequest后,我的超级方法有一个错误,我将如何解决?给我一个LoginRequest.javaI的完整代码我编辑了上面的答案,再次确认在android studio上所有{}和()都正确关闭,可能错过了一些是的,谢谢。我也使用了你的代码,但它不起作用。非常奇怪。它没有到达登录请求,而是跳过并进入队列。对此很抱歉,我想问题是队列在LoginActivity中,而请求在LoginRequest中,而事情并没有真正起作用。检查新答案,嗯。。。明文密码:(一般来说,使用JSON进行改型要比使用现成的凌空密码容易得多
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);
}
}