I'm trying to learn how to use JSON and I'm going over this code from a tutorial and there is a init() method. From what I found online init() is usually used as the entry point of applets. If so then why is init() in the android apps code and not the website code? Can someone please explain its reason? Is this something that is common when using JSON in android or is this uncommon?
public class MainActivity extends AppCompatActivity {
private RecyclerView mRestaurantRecyclerView;
private RestaurantAdapter mAdapter;
private ArrayList<Restaurant> mRestaurantCollection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
new FetchDataTask().execute();
}
private void init() {
mRestaurantRecyclerView = (RecyclerView) findViewById(R.id.restaurant_recycler);
mRestaurantRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRestaurantRecyclerView.setHasFixedSize(true);
mRestaurantCollection = new ArrayList<>();
mAdapter = new RestaurantAdapter(mRestaurantCollection, this);
mRestaurantRecyclerView.setAdapter(mAdapter);
}
public class FetchDataTask extends AsyncTask<Void, Void, Void> {
private String mZomatoString;
@Override
protected Void doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
Uri builtUri = Uri.parse(getString(R.string.zomato_api));
URL url;
try {
url = new URL(builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("user-key", "acfd3e623c5f01289bd87aaaff1926c1");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
//Nothing to do
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
mZomatoString = buffer.toString();
JSONObject jsonObject = new JSONObject(mZomatoString);
Log.v("Response", jsonObject.toString());
JSONArray restaurantsArray = jsonObject.getJSONArray("restaurants");
//list = new ArrayList<>();
for (int i = 0; i < restaurantsArray.length(); i++) {
Log.v("BRAD_", i + "");
String name;
String address;
String currency;
String imageUrl;
long lon;
long lat;
long cost;
float rating;
JSONObject jRestaurant = (JSONObject) restaurantsArray.get(i);
jRestaurant = jRestaurant.getJSONObject("restaurant");
JSONObject jLocattion = jRestaurant.getJSONObject("location");
JSONObject jRating = jRestaurant.getJSONObject("user_rating");
name = jRestaurant.getString("name");
address = jLocattion.getString("address");
lat = jLocattion.getLong("latitude");
lon = jLocattion.getLong("longitude");
currency = jRestaurant.getString("currency");
cost = jRestaurant.getInt("average_cost_for_two");
imageUrl = jRestaurant.getString("thumb");
rating = (float) jRating.getDouble("aggregate_rating");
Restaurant restaurant = new Restaurant();
restaurant.setName(name);
restaurant.setAddress(address);
restaurant.setLatitiude(lat);
restaurant.setLongitude(lon);
restaurant.setCurrency(currency);
restaurant.setCost(String.valueOf(cost));
restaurant.setImageUrl(imageUrl);
restaurant.setRating(String.valueOf(rating));
mRestaurantCollection.add(restaurant);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("MainActivity", "Error closing stream", e);
}
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
mAdapter.notifyDataSetChanged();
}
}