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. We briefly explored other data formats in our introduction to converters tutorial, but didn't go into more detail.
Learn how to receive and process plain-string responses from your API in this tutorial.
Retrofit Series Overview
- Getting Started and Creating an Android Client
- Basics of API Description
- Creating a Sustainable Android Client
- URL Handling, Resolution and Parsing
- How to Change API Base Url at Runtime
- Multiple Server Environments (Develop, Staging, Production)
- Share OkHttp Client and Converters between Retrofit Instances
- Upgrade Guide from 1.9
- Beyond Android: Retrofit for Java Projects
- How to use OkHttp 3 with Retrofit 1
- Synchronous and Asynchronous Requests
- Send Objects in Request Body
- Add Custom Request Header
- Manage Request Headers in OkHttp Interceptor
- Dynamic Request Headers with @HeaderMap
- Multiple Query Parameters of Same Name
- Optional Query Parameters
- Send Data Form-Urlencoded
- Send Data Form-Urlencoded Using FieldMap
- How to Add Query Parameters to Every Request
- Add Multiple Query Parameter With QueryMap
- How to Use Dynamic Urls for Requests
- Constant, Default and Logic Values for POST and PUT Requests
- Cancel Requests
- Reuse and Analyze Requests
- Optional Path Parameters
- How to Send Plain Text Request Body
- Customize Network Timeouts
- How to Trust Unsafe SSL certificates (Self-signed, Expired)
- Dynamic Endpoint-Dependent Interceptor Actions
- How to Update Objects on the Server (PUT vs. PATCH)
- How to Delete Objects on the Server
- Introduction to (Multiple) Converters
- Adding & Customizing the Gson Converter
- Implementing Custom Converters
- How to Integrate XML Converter
- Access Mapped Objects and Raw Response Payload
- Supporting JSON and XML Responses Concurrently
- Handling of Empty Server Responses with Custom Converter
- Send JSON Requests and Receive XML Responses (or vice versa)
- Unwrapping Envelope Responses with Custom Converter
- Wrapping Requests in Envelope with Custom Converter
- Define a Custom Response Converter
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 {
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.5.0'
// Retrofit Scalars Converter
compile 'com.squareup.retrofit2:converter-scalars:2.5.0'
}
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())
.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.
In the next tutorial we'll explore how you can crawl website sources with this method. You'll implement a Retrofit converter, which automatically maps from the HTML of a Wikipedia page to a useful Java object. Stay tuned!
Do you have further questions on this topic or about Retrofit in general? Let us know on Twitter @futurestud_io or leave a comment below.
Enjoy coding & make it rock!