首页 > 解决方案 > 如何获取服务器对在 Android Studio (Java) 中调用请求的方法的响应

问题描述

我正在使用两个类,第一个是我的主要活动,第二个是具有发送 HTTP Post 请求的方法的类 [分别为 Login.java 和 Postman.java]

我尝试通过创建的对象访问它,但是来自服务器的响应在 onResponse 方法/类中(不知道)

登录活动


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


}


邮递员.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);

    }
}


我需要将响应中的变量 stat 返回到登录活动类,以便可以在那里完成其余的工作。我的目标是通过每次将 url 和 body 传递给方法来使用单个类来发送 http 请求。

标签: javaandroidandroid-studio

解决方案


从这里使用我的模板:How to get data from any asynchronous operation in android,这就是你可以做的:

定义一个接口:

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

现在,将您的方法签名更改为:

public void sendPOST(final Context context, String url, JSONObject data, final ResponseCallback 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);

    }
}

现在,在您之前调用您的方法的地方,只需使用这个:

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

推荐阅读