Essential additional task that needs to be completed before the next step (caching).
**Current plan: **Start with 100m ggsradius and if <10 categories found, recurse with ggsradius of 10x the last one. There appears to be a limit of <=10,000m for the ggsradius (after which the API throws an error[0]) so stop at that point regardless.
[0]:
"error": {
"code": "ggsintegeroutofrange",
"info": "ggsradius may not be over 10000 (set to 10001) for users",
**2/1/16: **First draft of implementation done at https://github.com/nicolas-raoul/apps-android-commons/pull/36 , but there is an issue where categorySet hasn't actually been called yet when the categorySet.size()>=10 check to break out of the for loop is done, so it calls the API three times always. Probably needs to be handled differently.
**3/1/16: **I attempted to refactor the code according to Niedzielski's recommendations:
(1) API code needs to be refactored, ShareActivity.java should supply the Listener instead.
(2) For sending multiple API requests, RequestQueue should be refactored into a singleton pattern.
(3) The 'for' loop in my current code is running infinite loops because it isn't extracting the current request's radius. There is actually no need for a 'for' loop, we just need to check if there are enough categories and if the radius is within limits.
(4) If there are issues with static/non-static methods, refactor.
So I put the ResponseListener<T> class in ShareActivity.java, and passed it into the QueryRequest constructor. I wrote a VolleyRequestQueue class to implement the singleton pattern, according to [[ https://developer.android.com/intl/es/training/volley/requestqueue.html | this ]] tutorial. I rewrote MwVolleyApi.java (now called MwVolley.java) to try and avoid use of nested static classes where possible, as I was previously encountering issues with the methods in static classes not being able to access non-static variables or methods.
As the QueryResponse, Query, Page, and Category classes are all non static, I received the 'no arg constructor' error and fixed that by registering InstanceCreators for them as mentioned in [[ http://howtodoinjava.com/2014/06/17/google-gson-tutorial-convert-java-object-to-from-json/ | this ]] tutorial. That fixed that particular issue.
However, now I am encountering this [1] error:
[1]
```
01-03 04:08:06.443: E/ACRA(11486): fr.free.nrw.commons fatal error : Attempt to invoke virtual method 'int fr.free.nrw.commons.upload.MwVolley.getRadius()' on a null object reference
01-03 04:08:06.443: E/ACRA(11486): java.lang.NullPointerException: Attempt to invoke virtual method 'int fr.free.nrw.commons.upload.MwVolley.getRadius()' on a null object reference
01-03 04:08:06.443: E/ACRA(11486): at fr.free.nrw.commons.upload.ShareActivity$ResponseListener.onResponse(ShareActivity.java:226)
```
However I am pretty sure I have instantiated apiCall (the MwVolley object) in the onCreate() method of ShareActivity, and have also tried instantiating it in other places while receiving the same error. If I comment out the getRadius() statement and replace it with an arbitrary radius, it runs for a bit, prints [2], and then crashes with the error message [3]
[2]:
```
01-03 04:57:35.865: D/Cat(14858): gpsCatExists=true
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): query=pages=
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES= Category:2010 in New Zealand, Category:Construction sites in Auckland, Category:Footbridges in Auckland, Category:Keep left road signs in New Zealand, Category:Night in Auckland, Category:Red and white stripe traffic cones, Category:Remote views of the Sky Tower, Category:Roadworks in New Zealand, Category:Traffic cones in New Zealand, Category:Traffic signals in New Zealand,
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES= no categories exist
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES= no categories exist
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES= no categories exist
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES= no categories exist
01-03 04:57:35.866: D/fr.free.nrw.commons.upload.ShareActivity$ResponseListener(14858): CATEGORIES FOUND[2010 in New Zealand, Roadworks in New Zealand, Night in Auckland, Footbridges in Auckland, Remote views of the Sky Tower, Traffic signals in New Zealand, Keep left road signs in New Zealand, Red and white stripe traffic cones, Traffic cones in New Zealand, Construction sites in Auckland]
```
[3]:
```
01-03 04:57:36.228: E/ACRA(14858): fr.free.nrw.commons fatal error : Attempt to read from field 'java.util.Set fr.free.nrw.commons.upload.MwVolley.categorySet' on a null object reference
01-03 04:57:36.228: E/ACRA(14858): java.lang.NullPointerException: Attempt to read from field 'java.util.Set fr.free.nrw.commons.upload.MwVolley.categorySet' on a null object reference
01-03 04:57:36.228: E/ACRA(14858): at fr.free.nrw.commons.upload.ShareActivity$ResponseListener.onResponse(ShareActivity.java:230)
```
This is a pretty big refactor so perhaps even though the error is listed in ShareActivity, it may actually stem from problems with the way I wrote my MwVolley or VolleyRequestQueue classes instead, although I really don't know what is wrong. @Nicolas_Raoul or @Niedzielski , would appreciate it if you took a look at my [[ https://github.com/nicolas-raoul/apps-android-commons/pull/36 | current code ]]. Thanks!