'Android Studio returning list of objects from within try/catch block
I'm trying to create a method which can save/and or return a list of objects which are pulled from a database. I'm able to create the list within a try/catch block but I'm unable to use the list anywhere else.
...
List<UserData.Doormat> doormats;
public void getDoormat(Activity activity ,double lat, double lon){
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//Starting Write and Read data with URL
//Creating array for parameters
String[] field = new String[2];
field[0] = "latitude";
field[1] = "longitude";
//Creating array for data
String[] data = new String[2];
data[0] = String.valueOf(lat);
data[1] = String.valueOf(lon);
PutData putData = new PutData("http://url", "POST", field, data);
if (putData.startPut()) {
if (putData.onComplete()) {
String result = putData.getResult();
try {
extractDoormat(result);
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("PutData", result);
}
}
//End Write and Read data with URL
}
});
}
This method is making a post to a database to return a list of database items which exist within a certain radius of the provided lat/lon values. The extractDoormat() method is below:
public void extractDoormat(String result) throws JSONException {
JSONObject obj = new JSONObject(result);
UserData user_data = (UserData) new Gson().fromJson(obj.toString(), UserData.class);
doormats = user_data.getData();
}
doormats is a global variable in the class List<UserData.Doormat> doormats . I can perform operations on the doormats list within the extractDoormat method, but no where else. I could probably combine these into one method, but even so, I cannot retrieve the list of objects outside of the try/catch block. I would like to use this class inside of other activities to retrieve the list of objects and manipulate them in those activities. Any help is greatly appreciated, thank you!
I have tried to combine the methods into one, but I am unsure of how to save the list of objects outside of this try/catch block.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
