在匿名内部类中设置一个最终变量

时间:2017-11-20 16:11:19

标签: android

我正在尝试使用使用匿名内部类的方法进行Web调用。我似乎无法将Web调用的结果分配给在内部类之外声明的变量。

我理解为什么变量需要是final的,因为如果调用web调用方法的东西被破坏,那么内部类就会引用一个不再存在的变量。

编译器说明:

cannot assign value to final variable 'result'

有人可以告诉我一个解决方法,将webcall的结果分配给结果变量吗?

public String makeWebApiPost(AppObj appObj, String url, JSONObject jsonObject ) {

        Log.e(TAG, "inside makeWebApiPost. url = " + url + " JSONObject = " + jsonObject.toString());


         final String result;



        String accessToken = appObj.dbModel.getAccessTokenFromDB();
        String refreshToken = appObj.dbModel.getRefreshTokenFromDB();

        if (! accessToken.equalsIgnoreCase("NO_ACCESSTOKEN")) {

            Log.e(TAG, "we have access tokens");


            StringRequest request = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response) {
                            // response
                            Log.e("Response", response);


                            try {


                                JSONObject obj = new JSONObject(response);
                                Log.e(TAG, "JSON obj from get tokens = " + obj.toString());

                                result = obj.toString();





                            }catch (Exception e){}

                        }
                    },


                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // error
                            Log.e("Error.Response", error.networkResponse.toString());
                            listener.getResult(null);
                        }
                    }
            ) {



           /* @Override
            public Map<String, String> getHeaders() throws AuthFailureError {


                Map<String,String> params = new HashMap<>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                //..add other headers
                return params;
            }*/


                @Override
                public String getBodyContentType() {
                    return "application/x-www-form-urlencoded";
                }

                @Override
                protected Map<String, String> getParams()
                {
                    Map<String, String>  params = new HashMap<String, String> ();
                    params.put("scope", "openid email phone profile offline_access roles");
                    params.put("resource", "https://lw.xxx.co.uk");
                    params.put("grant_type", "password");
                    params.put("username", userName);
                    params.put("password", password);


                    return params;
                }


                @Override
                protected VolleyError parseNetworkError(VolleyError response) {


                    try {

                        String json = new String(response.networkResponse.data, HttpHeaderParser.parseCharset(response.networkResponse.headers));
                        Log.e(TAG, "reponse error = " + json);

                    }catch (Exception e){}



                    return super.parseNetworkError(response);


                }

            };

            requestQueue.add(request);





        }

        return null;

    }//end of makeWebApiPost

1 个答案:

答案 0 :(得分:0)

您需要将result声明为类实例变量(或至少在方法的外部范围.... onResponse可以,并且很可能在方法返回后异步调用。