Mobile Apps 2023W Lecture 8: Difference between revisions
|  Created page with "==Resources==  Documentation: * [https://google.github.io/volley/ Volley] * [https://www.weatherbit.io/ Weatherbit] - create an account, sign up for the free plan  Tutorials (From GeeksforGeeks.org): * [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]" | 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] | ||
| ==Code== | |||
| <syntaxhighlight lang="kotlin" line> | |||
| 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) | |||
| } | |||
| </syntaxhighlight> | |||
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):
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)
}