Android on Response

Asked 2 years ago, Updated 2 years ago, 25 views

login_button = findViewById( R.id.login_button );
        login_button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println ("login button pressed");
                String UserNumber = login_number.getText().toString();
                String UserPwd = login_password.getText().toString();
                System.out.println (UserNumber+UserPwd+"Academic and password included");

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("onResponse");
                        try {
                            System.out.println (response+"get login value");
                            JSONObject jsonObject = new JSONObject(response);
                            boolean success = jsonObject.getBoolean( "success" );

                            if(success) {// Successful login
                                String UserNumber = jsonObject.getString( "UserNumber" );
                                String UserPwd = jsonObject.getString( "UserPwd" );
                                String UserName = jsonObject.getString( "UserName" );

                                Toast.makeText(getApplicationContext(), String.format("%s welcome", UserName), Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent( LoginActivity.this, VoteListActivity.class );

                                intent.putExtra( "UserNumber", UserNumber );
                                intent.putExtra( "UserPwd", UserPwd );
                                intent.putExtra( "UserName", UserName );

                                startActivity( intent );

                            } else {//if login fails
                                Toast.makeText(getApplicationContext(), "You failed to log in.", Toast.LENGTH_SHORT).show();
                                return;
                            }

                        } } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };


                LoginRequest loginRequest = new LoginRequest( UserNumber, UserPwd, responseListener );
                RequestQueue queue = Volley.newRequestQueue( LoginActivity.this );
                queue.add( loginRequest );

            }
        });
    }

package com.example.myapplication.Activity;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class LoginRequest extends StringRequest {

    //Set server URL (php file integration)
    final static private String URL = "http://voting.dothome.co.kr/Login.php";
    private Map<String, String> map;

    public LoginRequest(String UserNumber, String UserPwd, Response.Listener<String> listener) {
        super(Method.POST, URL, listener, null);

        map = new HashMap<>();
        map.put("UserNumber", UserNumber);
        map.put("UserPwd", UserPwd);

        System.out.println(map+"1");
    }

    @Override
    protected Map<String, String>getParams() throws AuthFailureError {
        System.out.println(map+"2");
        return map;
    }
}

When the login button (login_button) is pressed, onResponse is not executed, but there is no error and the data is easily transferred I don't know what the error is, but please help me...!

android

2022-09-20 16:32

1 Answers

The data went well, but the Response returned by the server does not seem to be properly handled by Response.Listener<String>.

Response.ErrorListener It seems that you are not implementing this part and processing it as null, but I think you should implement that part and check the error log for exactly what reason it cannot be processed as a normal response.


2022-09-20 16:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.