-
Notifications
You must be signed in to change notification settings - Fork 1
Internet OkHTTP
- Does a lot on efficiency
- Generally has nice documentation
- Has nice recipes to cut and paste
- Requirements are clear.
- Under releases, it has clear dependency information to put into build.gradle:
implementation("com.squareup.okhttp3:okhttp:4.2.1") - Has information for creating a mock web server so OkHTTP can talk to the mock web server, so the test doesn't have external dependencies.
Create an OkHTTP client, we create a new request, we use our client to create a call for that request, and enqueue it by passing in what we want when we're done. This is asynchronous running on a background thread.
Note that there are no databases in this particular version being used for the demo.
Go to MainActivity and paste in:
OkHttpClient client = new OkHttpClient();
and put in a Request builder. Also put in an execute... but will need to put it in a catch try block, or an exception.
- Try with resources is what the try/catch block is called when there are things like Response response, etc in the parens.
Open up logcat and put in package name to watch logs. Remember to check the run tab to see why things aren't working... such as it not allowing network on main thread.
Checkout Asynchronous Get(.kt, .java) in docs, where you download a file on a worker thread to deal with threads. Create a callback function, called LogDataWhenItComeBackCallback.
Note that android apps need permissions! Such as to use the internet. Go to manifests folder. This is where we specify things about the app that are needed for android before our app is even installed in our phone. In here is where we add permissions... specifically INTERNET permission.
<uses-permission android:name="android.permission.INTERNET"/>
We create a handler for the main thread and then created a message that is sent to the handler that includes the data we are interested in.
Create a method called putDataOnPage, that takes in a String data.
We'll have the data fill in a textview. So, grab the textview and set the text data. Now we can call this on an instance of Main Activity.
We need to give our callback knowledge of the main activity now. Create an instance of Main Activity. The main activity creates the callback, and says you can send me data by remembering me.
- Note that you can only call .string() once, otherwise you get an error! Just turn it into a string once, and hold onto it. So, create a variable.
- we need to use a handler, which acts as an intermediary between threads... which we can do in onResponse method with handlerForMainThread.
Can either put google.com html on the android app... or something more useful, such as from an API! Like numbersapi.com... but see below in security, since this is an http site.
Security - http versus https:
- To make request to places that don't have https, note that there is another permission to add. Otherwise, there is an error called CLEARTEXT communication that pops up.
- OkHTTP
- calls OkHttp to enqueue request
- On response, creates Handler to interact with main thread
- tells handler to call putDataOnPage on our MainActivity
- to turn JSON string into Task instances
- Set Task instances as its data source in the Adapter
- Adapter sets up "this is the data that the recycler view shows"