如何在方法参数或不同解决方案中传递Java方法

时间:2018-04-17 00:51:36

标签: java android http

我正在使用创建移动应用程序。此应用程序的目的是复制现有网站上使用的流程(出于隐私原因无法披露网站)

当使用参数usr(用户名)和pwd(密码)的请求时,网站当前返回JSON对象。例如:

www.website.com/checkLogin.php?usr=username&pwd=password

当前工作系统是应用程序主要活动类中的一个函数:

public void SimpleGetRequest(String url){

    // Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(this);

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    // display the response for bug testing
                    Log.d("CREATION", "Response: " + response);

                    //ChangeText(response);

                    //Validate the JSON oBject that should be returned
                    if(ValidateLogin(response)){
                        // change the temp username and password in globals on static method
                        setUsernameAndPassword();

                        // change text to show success
                        ChangeText("Login Success");

                        // change ethe activity to the home page on the login screen
                        ChangeActivity();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            ChangeText("Couldn't Connect to Server");
            Log.d("CREATION", "Request Failed");
        }
    });

    // Add the request to the RequestQueue.
    queue.add(stringRequest);
}

此方法接受url输入并运行一个简单的请求,然后在其自身内部创建的onResponse()函数中返回响应。这适用于我的目的但是我想在其他活动中使用此方法,这需要我将其复制并粘贴到其他活动中。相反,我想创建一个静态类,该活动引用此方法。但是因为响应驻留在方法内部的回调函数中,所以我无法使用该方法返回任何内容。

我认为可能的解决方案是使用SimpleRequest()方法从活动传递函数,因此当调用回调函数时,它会将结果发送回活动,以便可以使用它而不是直接放置函数进入回调函数。

有可能或这种方法存在缺陷吗?

0 个答案:

没有答案