添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • Obtain the dependencies for Retrofit2 using Gradle
  • Create the model for the API request and response
  • Create a Java interface representing the service and including Retrofit2 annotations
  • Set up the HTTP client using a singleton pattern within your app
  • Invoke the HTTP client generating the request and handling the response and errors
  • Configure the app manifest to give permission to access the internet
  • Three fields for capturing a Comment including a title, a body and an author
  • A radio button group containing three different options for creating the HTTP POST request using Retrofit2
  • And a button to send the HTTP POST request containing the data about the Comment
  • compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8

    OkHttp3

    OkHttp3 is a HTTP client for Java and Kotlin developed by Square.

    Line 13, contains both the request method and path that will be called on the API when the createComment(Comment) method is invoked. In this case we are using a POST request so we use the “@POST” Retrofit2 annotation. We will be sending the POST request to the “/comments” end point on the API so inside we pass the path as a parameter to the “@POST” annotation.

    The full list of request methods supported by Retrofit2 are:

    On line 14, we are defining the method for creating a comment and we are passing a @Body annotation with a Comment object as a parameter. Providing an @Body annotation with a Comment as a parameter indicates that we will be providing a request body when we invoke this API which in our case will be a JSON payload containing the details of the comment.

    The finally, the createComment(Comment) method returns a Call<Comment> object. This means that when we invoke the API to create the comment we expect to see a request body containing a JSON body that we will deserialize into a Comment object (in our case using Gson).

    Step 4: Setting Up the HTTP Client

    In this part of the tutorial we will setting up access to the HTTP client using Retrofit2 inside a repository class that follows the singleton pattern. This will enable us in the next step of tutorial to retrieve the instance of the repository then invoke the API to create a comment.

    Please see the code below containing the CommentsRepository class which initialises the HTTP client using Retrofit2 inside the constructor.

    One final thing you will need to do to ensure you have connectivity to the API is to enable the permission to access the internet (android.permission.INTERNET) inside your Android Manifest for your app.

    In addition to this, if you are going to use the Flask API I wrote and don’t want to configure a HTTPS certificate for the API, you will need to add an attribute in the application tag (by setting “android:usesCleartextTraffic” to “true”) inside the Android Manifest to specify that you want to send clear text traffic in your Android app. Warning: Sending clear text traffic should not be done in production for security reasons.

    See the sample AndroidManifest.xml file below the shows how to configure the use of clear text traffic and setting the permission to access the internet.

    If you have a look at lines 83 to 87 in the activity code shared in Step 5 of this tutorial you will see that instead of passing the Comment object as a parameter to the createComment method, a HashMap is passed as an parameter. This HashMap contains key value pairs of which the field names are defined as the keys and the comment title, comment body and comment author are defined as the values.

    Randomness is required in a wide variety of different Android applications. For example, if you wanted your Android app to roll a die, flip a coin, or draw a card from a shuffled deck of cards, you...

    About Us

    LearnToDroid is an online resource focused on providing quality content that helps our readers learn how to design, create and market outstanding apps for Android.

    Social Accounts