Skip to main content

Retrofit 2 — Receive Plain-String Responses

Android apps usually interact with REST APIs, which often use JSON as a data format. We've focused almost all of our tutorials on sending JSON or XML requests, and converting JSON or XML responses.


Scalars Converter for Plain Strings

To receive plain-text or plain-string responses you can utilize the scalars converter. You can integrate the converter by adding it as a dependency to your app's build.gradle:


dependencies {  

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'

    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'//You need to add this

    implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2'

}


Next, you need to describe the endpoint you want to interact with. In this demo case, you'll use a GET request with the dynamic URL feature to pass any URL to the method, and set the response type as String.


@GET()

Call<String> getStringResponse(@Url String url);


You've finished the preparations. Next, you can use Retrofit to execute requests with the scalars converter and the created endpoint. You'll create a Retrofit object with the Retrofit builder and configure it to use the scalars converter:


Retrofit retrofit = new Retrofit.Builder()  

        .addConverterFactory(ScalarsConverterFactory.create())//You need to add this

        .addConverterFactory(GsonConverterFactory.create())

        .baseUrl("https://your.base.url/")

        .build();

The final step is to use the Retrofit object to execute a request:


ScalarService scalarService = retrofit.create(ScalarService.class);  

Call<String> stringCall = scalarService.getStringResponse("https://futurestud.io");  

stringCall.enqueue(new Callback<String>() {  

    @Override

    public void onResponse(Call<String> call, Response<String> response) {

        if (response.isSuccessful()) {

            String responseString = response.body();

            // todo: do something with the response string

        }


    }


    @Override

    public void onFailure(Call<String> call, Throwable t) {


    }

});


In the code snippet above, you're executing a GET request for our homepage futurestud.io. Consequently, you'll receive a long string back (which is actually the website's HTML source). You can also use the above approach to receive plain strings from your API.


Summary

In this tutorial you've learned how you can set up Retrofit to access plain-string responses from requests. This could be a regular API call, where the server returns a string. But this could also be the HTML source of a website.

Do you have further questions on this topic or about Retrofit in general? Let us leave a comment below.


Enjoy coding & make it rock!

Comments

Popular posts from this blog

What is android?

  Android is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones etc. Or We can say, android is a Linux-Based Operating System. It is currently used in various devices such as mobiles, tablets, televisions etc.

What is the Android Architecture?

 Well, Android Architecture is categorized into five part.     1. Linux kernel     2. Native Libraries (Middleware),     3. Android Runtime     4. Application Framework     5. Applications You can see the picture shown below. Linux kernel: It is the heart of android architecture that exists at the root of android architecture. Linux kernel is responsible for device drivers, power management, memory management, device management and resource access. Native Libraries: On the top of Linux kernel, their are Native libraries such as Web-Kit, Open-GL, Free-Type, SQLite, Media, C runtime library (libc) etc. The Web-Kit library is responsible for browser support, SQLite is for database, Free-Type for font support, Media for playing and recording audio and video formats. Android Runtime: In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is responsible to run android application. DVM...

How to call Post type API using Retrofit in Android?

Step by Step Implementation Step 1: Create a New Project Step 2: Add the below dependency in your build.gradle file Navigate to the Gradle Scripts > build.gradle (Module:app) and add the below dependency in the dependencies section.  // below dependency for using the retrofit implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.5.0’ After adding this dependency sync your project and now move towards the AndroidManifest.xml part.   Step 3: Adding permissions to the internet in the AndroidManifest.xml file Navigate to the app > AndroidManifest.xml and add the below code to it.  <!--permissions for INTERNET--> <uses-permission android:name="android.permission.INTERNET"/> Step 4: Working with the activity_main.xml file Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.  <?xml version="1.0"...