Skip to content

Instantly share code, notes, and snippets.

@mkantar
Created April 30, 2017 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkantar/33a2c44f75f9cbdfdd5e2e51ee0db590 to your computer and use it in GitHub Desktop.
Save mkantar/33a2c44f75f9cbdfdd5e2e51ee0db590 to your computer and use it in GitHub Desktop.
Sending location data to the server
public class Information {
public String title;
public String description;
public String imageUrl;
public String label;
public static ArrayList<Information> getRecipesFromFile(String filename, Context context){
final ArrayList<Information> informationList = new ArrayList<>();
try {
// Load data
String jsonString = loadJsonFromAsset("ali.json", context);
JSONObject json = new JSONObject(jsonString);
JSONArray recipes = json.getJSONArray("ali");
// Get Information objects from data
for(int i = 0; i < recipes.length(); i++){
Information information = new Information();
information.title = recipes.getJSONObject(i).getString("title");
information.description = recipes.getJSONObject(i).getString("description");
information.imageUrl = recipes.getJSONObject(i).getString("image");
information.label = recipes.getJSONObject(i).getString("dietLabel");
informationList.add(information);
}
} catch (JSONException e) {
e.printStackTrace();
}
return informationList;
}
private static String loadJsonFromAsset(String filename, Context context) {
String json = null;
try {
InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
}
catch (java.io.IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment