简体   繁体   中英

How to get back server response to a method from which the request was invoked in Android Studio (Java)

I'm using two classes, the first one is my main activity and the second is a class with a method which sends an HTTP Post request [Login.java and Postman.java respectively]

I have tried accessing it through the created object, but the response from the server is inside the onResponse method/class (don't know about it)

Login Activity


public class Login extends AppCompatActivity {
    EditText usr,passwd;
    TextView error;
    Button login;

    public void start_login()
    {
        String url ="http://example.com/signin.php";
        String username = usr.getText().toString();
        String password = usr.getText().toString();
        JSONObject data = new JSONObject();
        try
        {
            data.put("Username", username);
            data.put("Password", password);
        }
        catch (Exception e){e.printStackTrace();}
        Postman send_object = new Postman();
        send_object.sendPOST(getApplicationContext(),url,data);
    }


}


Postman.java


public class Postman
{
    String stat;
    public void sendPOST(final Context context, String url, JSONObject data)
    {
        RequestQueue req_q = Volley.newRequestQueue(context);
        JsonObjectRequest data_req = new JsonObjectRequest(Request.Method.POST, url, data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response)
            {
                try
                {
                    stat = response.getString("Status");
                }catch (Exception e){e.printStackTrace();}
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error)
            {

            }
        });
        req_q.add(data_req);

    }
}


I need the variable stat from the response back to the login activity class so that the rest can be done there. I aim to use a single class for sending http requests by passing the url and body each time to the method.

using my template from here : How to get data from any asynchronous operation in android , this is what you can do :

define an interface :

public interface ResponseCallback {
    void myResponseCallback(String result);
}

now, change your method signature to this :

public void sendPOST(final Context context, String url, JSONObject data, final ResponseCallback callback)

implement the callback :

  public void sendPOST(final Context context, String url, JSONObject data, final ResponseCallback callback)
    {
        RequestQueue req_q = Volley.newRequestQueue(context);
        JsonObjectRequest data_req = new JsonObjectRequest(Request.Method.POST, url, data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response)
            {
                try
                {
                    String stat  = response.getString("Status");
                    callback.myResponseCallback(stat);
                }catch (Exception e){e.printStackTrace();}
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error)
            {

            }
        });
        req_q.add(data_req);

    }
}

now, where you previously called your method, just use this :

  send_object.sendPOST(getApplicationContext(), url, data, new ResponseCallback() {
            @Override
            public void myResponseCallback(String result) {
 //here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do.
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM