Android Volley: Static vs Object

后端 未结 2 793
春和景丽
春和景丽 2020-12-21 17:04

I am a junior android developer and I almost finished the alpha version of my first big project. I think that I have good knowledge of java but I am not sure if I organized

相关标签:
2条回答
  • 2020-12-21 17:43

    No, in your case, both ways will have the same result...

    The only thing to mention is, that if you need to receive the response to your request too (may be in the future), you will need to add a Delegate / Callback / Interface to your class, to get the result right back to your calling activity instance... In that case it would be better to create a "non-static instance method" way... But you can add a non-static Method to your Class too so I don't see anything against it.

    UPDATE TO COMMENT

    Well for example, if you want to provide a ListView with Images... In most cases you first request an JSONArray with your ListView entries, which contains the links to Bitmaps located on the remote Server...

    If you download Images Async and put them into the ImageViews in the rows of a ListView (while the user scrolls), it could be possible that images are loaded longer and the ListView will show images in wrong places... For something like that you will need a Singleton Pattern, which will manage the downloads for you... This will not be possible with your class/static Method

    0 讨论(0)
  • 2020-12-21 17:45

    Although this question has already had an accepted answer, however, I'd like to share my code that looks like your issue. Hope this helps!

    I also use Interface like @Neo answer, as the following:

    public interface VolleyResponseListener {
        void onError(String message);
    
        void onResponse(Object response);
    }
    

    Then in my VolleyUtils class:

    public static void makeJsonObjectRequest(Context context, String url, final VolleyResponseListener listener) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                    (url, null, new Response.Listener<JSONObject>() {
    
                    @Override
                    public void onResponse(JSONObject response) {
                        listener.onResponse(response);
                    }
                }, new Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        listener.onError(error.toString());
                    }
                }) {
    
            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
                    return Response.success(new JSONObject(jsonString),
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (JSONException je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
    
        // Access the RequestQueue through singleton class.
        VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
    }
    

    Then in Activity:

    VolleyUtils.makeJsonObjectRequest(mContext, url, new VolleyResponseListener() {
            @Override
            public void onError(String message) {
    
            }
    
            @Override
            public void onResponse(Object response) {
    
            }
        });
    

    P/S: my project uses Google's official Volley library, instead of using compile 'com.mcxiaoke.volley:library:1.0.17' in build.gradle. As a result, JsonObjectRequest(...) will have a difference at its definition.

    0 讨论(0)
提交回复
热议问题