Created
June 15, 2018 10:09
-
-
Save hackertron/d5f49f67b1b00da1fba754817d9477a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fire.fire.pullingjsonresponseintorecyclerviewtutorial; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import com.fire.fire.pullingjsonresponseintorecyclerviewtutorial.adapters.RestaurantAdapter; | |
import com.fire.fire.pullingjsonresponseintorecyclerviewtutorial.model.Restaurant; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment