Basic API implementation#4
Conversation
-dependency added for internet connection - thread created for API endpoint which has request GET method
There was a problem hiding this comment.
Pull Request Overview
Implements a basic network call for fetching recipes and sets up the main activity with Compose UI.
- Add MainActivity with a fetchRecipes() method using HttpURLConnection and Gson
- Add INTERNET permission in AndroidManifest
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| MainActivity.kt | Adds Compose UI scaffolding and a background-threaded network call parsing JSON via Gson |
| AndroidManifest.xml | Declares INTERNET permission and app/activity configuration (contains unresolved merge conflict) |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| <<<<<<< HEAD | ||
|
|
||
| //dependecy added for internent connection | ||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
|
|
||
| ======= | ||
| <uses-permission android:name="android.permission.INTERNET"/> | ||
| >>>>>>> 2adbb52ebf87dfede19d67b9789c30621982a91e |
There was a problem hiding this comment.
Unresolved merge conflict markers (<<<<<<<, =======, >>>>>>>) are present and will break the build. Also, the // comment is invalid in XML and the INTERNET permission is duplicated. Resolve the conflict, remove the invalid comment, and keep a single <uses-permission android:name="android.permission.INTERNET" /> line.
| <<<<<<< HEAD | |
| //dependecy added for internent connection | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| ======= | |
| <uses-permission android:name="android.permission.INTERNET"/> | |
| >>>>>>> 2adbb52ebf87dfede19d67b9789c30621982a91e | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| private lateinit var binding: ActivityMainBinding | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) |
There was a problem hiding this comment.
Both setContentView and setContent are called, which mixes ViewBinding and Compose rendering paths in the same Activity. If you're using Compose, remove setContentView/binding and do all UI in setContent (or, alternatively, remove setContent and use only view binding).
| private lateinit var binding: ActivityMainBinding | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| binding = ActivityMainBinding.inflate(layoutInflater) | |
| setContentView(binding.root) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) |
| //API endpoint | ||
| val url = URL("https://api.spoonacular.com/recipes/random?number=10&apiKey=YOUR_API_KEY") | ||
|
|
||
| val connection = HttpURLConnection = url.openConnection() as HttpURLConnection |
There was a problem hiding this comment.
This is invalid Kotlin and shadows the HttpURLConnection type. It should be assigning the result of openConnection to a variable: val connection = url.openConnection() as HttpURLConnection.
| val connection = HttpURLConnection = url.openConnection() as HttpURLConnection | |
| val connection = url.openConnection() as HttpURLConnection |
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import com.example.easychef.ui.theme.EasyChefTheme |
There was a problem hiding this comment.
Required imports for the networking and parsing code are missing, causing compilation errors. Add imports for java.net.URL, java.net.HttpURLConnection, java.io.BufferedReader, java.io.InputStreamReader, com.google.gson.Gson, and your view binding type (com.example.easychef.databinding.ActivityMainBinding).
| import com.example.easychef.ui.theme.EasyChefTheme | |
| import com.example.easychef.ui.theme.EasyChefTheme | |
| import java.net.URL | |
| import java.net.HttpURLConnection | |
| import java.io.BufferedReader | |
| import java.io.InputStreamReader | |
| import com.google.gson.Gson | |
| import com.example.easychef.databinding.ActivityMainBinding |
| if (responseCode == HttpURLConnection.HTTP_OK) { | ||
| val inputStream = connection.inputStream | ||
| //Read and print the response data | ||
| val reader : BufferedReader = BufferedReader(InputStreamReader(inputStream)) | ||
| val response = StringBuilder() | ||
| var line: String? | ||
| while (reader.readLine().also { line = it } != null) { | ||
| response.append(line) | ||
| } | ||
|
|
||
| //close connections | ||
| reader.close() | ||
| inputStream.close() | ||
| connection.disconnect() | ||
|
|
||
| //JSON response via Gson | ||
| val jsonResponse = response.toString() | ||
| val gson = Gson() | ||
| val recipeResponse = gson.fromJson(jsonResponse, RecipeResponse::class.java) |
There was a problem hiding this comment.
Streams and the connection are only closed on the success path; failures or exceptions will leak resources. Wrap I/O in use blocks and disconnect in a finally block, e.g., reader.use { ... } and always call connection.disconnect() in finally.
| private fun fetchRecipes(): Thread{ | ||
| return Thread{ | ||
| //API endpoint | ||
| val url = URL("https://api.spoonacular.com/recipes/random?number=10&apiKey=YOUR_API_KEY") |
There was a problem hiding this comment.
Avoid hardcoding API keys in source code. Store secrets outside the repo (e.g., local.properties/Gradle) and read at runtime via BuildConfig or from encrypted storage.
| private fun fetchRecipes(): Thread{ | ||
| return Thread{ | ||
| //API endpoint | ||
| val url = URL("https://api.spoonacular.com/recipes/random?number=10&apiKey=YOUR_API_KEY") | ||
|
|
There was a problem hiding this comment.
[nitpick] Prefer Kotlin coroutines (Dispatchers.IO) and a modern HTTP client (OkHttp/Retrofit) over manually managing Thread and HttpURLConnection; this simplifies cancellation, error handling, and lifecycle awareness. Consider making this a suspend function and calling it from lifecycleScope.
| val connection = HttpURLConnection = url.openConnection() as HttpURLConnection | ||
|
|
||
| //Request GET Method | ||
| connection.requestMethod = "GET" |
There was a problem hiding this comment.
[nitpick] Prefer Kotlin coroutines (Dispatchers.IO) and a modern HTTP client (OkHttp/Retrofit) over manually managing Thread and HttpURLConnection; this simplifies cancellation, error handling, and lifecycle awareness. Consider making this a suspend function and calling it from lifecycleScope.
| private fun fetchRecipes(): Thread{ | ||
| return Thread{ |
There was a problem hiding this comment.
fetchRecipes returns a Thread but is never called, and the returned thread is not started. Either start the thread where needed, or refactor to a suspend function and invoke it from a scoped coroutine.
| <<<<<<< HEAD | ||
|
|
||
| //dependecy added for internent connection | ||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
|
|
||
| ======= | ||
| <uses-permission android:name="android.permission.INTERNET"/> | ||
| >>>>>>> 2adbb52ebf87dfede19d67b9789c30621982a91e |
There was a problem hiding this comment.
Corrected spelling.
| <<<<<<< HEAD | |
| //dependecy added for internent connection | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| ======= | |
| <uses-permission android:name="android.permission.INTERNET"/> | |
| >>>>>>> 2adbb52ebf87dfede19d67b9789c30621982a91e | |
| <!-- Dependency added for internet connection --> | |
| <uses-permission android:name="android.permission.INTERNET" /> |
-dependency added for internet connection