Mobile Apps 2023W Lecture 8: Difference between revisions
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
* [https://www.geeksforgeeks.org/volley-library-in-android/ Volley Tutorial] | * [https://www.geeksforgeeks.org/volley-library-in-android/ Volley Tutorial] | ||
* [https://www.geeksforgeeks.org/how-to-build-a-weather-app-in-android/ Weather App Tutorial] | * [https://www.geeksforgeeks.org/how-to-build-a-weather-app-in-android/ Weather App Tutorial] | ||
==Notes== | |||
<pre> | |||
Lecture 8 | |||
--------- | |||
Playing with REST APIs in Android | |||
Resources are on the wiki. | |||
Steps: | |||
* make an empty Compose project (I'm doing it at API 31 level) | |||
* Add Internet permissions in AndroidManifest.xml: | |||
<uses-permission android:name="android.permission.INTERNET" /> | |||
(put inside <manifest> at the top level) | |||
* Add Volley to the project: | |||
edit build.gradle (Module: <app name>) | |||
in the dependencies {} section at the bottom, add: | |||
implementation("com.android.volley:volley:1.2.1") | |||
(make sure to context select to sync this after adding this) | |||
</pre> | |||
==Code== | ==Code== |
Latest revision as of 23:37, 3 February 2023
Resources
Documentation:
- Volley
- Weatherbit - create an account, sign up for the free plan
Tutorials (From GeeksforGeeks.org):
Notes
Lecture 8 --------- Playing with REST APIs in Android Resources are on the wiki. Steps: * make an empty Compose project (I'm doing it at API 31 level) * Add Internet permissions in AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET" /> (put inside <manifest> at the top level) * Add Volley to the project: edit build.gradle (Module: <app name>) in the dependencies {} section at the bottom, add: implementation("com.android.volley:volley:1.2.1") (make sure to context select to sync this after adding this)
Code
package carleton.comp2601.weather2601
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import carleton.comp2601.weather2601.ui.theme.Weather2601Theme
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(this)
setContent {
Weather2601Theme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
ShowTemp(queue)
}
}
}
}
}
@Composable
fun ShowTemp(queue: RequestQueue) {
var temp by remember {
mutableStateOf("No Temp Yet")
}
getTemp({newTemp -> temp = newTemp}, queue)
Text(text = temp)
}
fun getTemp(updater: (String)->Unit, queue: RequestQueue) {
val weatherbitKey = "648ba46141624d01a4c4fc5c7b2be856"
val url: String = "https://api.weatherbit.io/v2.0/current?" +
"&city=Nashville&state=TN&country=USA" +
"&key=" + weatherbitKey
val stringReq = StringRequest(
Request.Method.GET, url,
{ response ->
val obj = JSONObject(response)
val arr = obj.getJSONArray("data")
val data = arr.getJSONObject(0)
val newTemp = data.getString("temp")
updater("Temp in Nashville: " + newTemp)
},
{ updater("Couldn't get temp")})
queue.add(stringReq)
}