Web Services APIs Call Flashcards

1
Q

Retrofit vs. OkHttp ?

A

The reason is simple: OkHttp is a pure HTTP/SPEEDY client responsible for any low-level network operation, caching, request and response manipulation, and many more.

In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit 2 is strongly coupled with OkHttp and makes intensive use of it.

OkHttp Functions: Connection pooling, gzipping, caching, recovers from network problems, sync, and async calls, redirects, retries … and so on.

Retrofit Functions: URL manipulation, requesting, loading, caching, threading, synchronization… It allows sync and async calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you explain the architecture of Retrofit?

A

Retrofit is a type-safe HTTP client for Android and Java. It makes it easy to consume JSON or XML data from a web service. It is built on top of the OkHttp library. Retrofit uses annotations to define how to map an endpoint to a Java interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the advantages and disadvantages of using Retrofit?

A

The main advantage of using Retrofit is that it makes it easy to connect to a REST API and retrieve data. The main disadvantage is that it can be difficult to set up, especially if you are not familiar with REST APIs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between a GET request and a POST request?

A

A GET request is a request for data from a server. A POST request is a request to create or update data on a server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you give me an example of how to use @Path annotation with Retrofit?

A

The @Path annotation is used to specify a path parameter in the URL. For example, if you have a URL like http://example.com/users/{userId}, then you can use @Path(“userId”) to specify that the userId parameter should be replaced with the actual value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the @QueryMap annotation do in Retrofit?

A

The @QueryMap annotation allows you to specify a map of query parameters for a request. This is useful for cases where you want to specify a large number of optional parameters for a request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some ways to handle error conditions when using Retrofit?

A

There are a couple of ways to handle error conditions when using Retrofit. One way is to use a custom ErrorHandler. This will give you the ability to handle errors in a way that makes sense for your application. Another way to handle errors is to use the @Retry annotation. This will cause Retrofit to automatically retry the request a specified number of times before giving up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you add custom headers to your requests using Retrofit?

A

By default, Retrofit will add any headers specified in your @Headers annotation to every request. If you want to add headers only for a single request, you can use the @Headers annotation on that request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you set up multiple endpoints for your API calls using Retrofit?

A

You can set up multiple endpoints for your API calls using Retrofit by creating a new instance of the Retrofit class for each endpoint. For example, if you have two endpoints, one for production and one for development, you would create two Retrofit instances, one for each endpoint. You would then create a new Service interface for each endpoint, and each Service would have its own methods for making API calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is it possible to define multiple base URLs for your application? If yes, then how?

A

Yes, it is possible to define multiple base URLs for your application using Retrofit. This can be accomplished by using the @Url annotation on your Retrofit interface methods. This annotation takes a String value which represents the URL that you want to use for the specific method call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do you think you might need to create custom converters?

A

There are a few reasons you might need to create custom converters. One reason is if you are working with a legacy API that doesn’t return data in a format that Retrofit can work with by default. Another reason is if you want more control over how the data is converted from its raw format into Java objects. Creating custom converters gives you the ability to fine-tune this process to better suit your needs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the best way to parse XML responses from a server with Retrofit?

A

The best way to parse XML responses from a server with Retrofit is to use a SimpleXmlConverter. This converter will take care of all of the heavy lifting for you and will allow you to focus on other aspects of your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you handle authentication when using Retrofit?

A

When using Retrofit for authentication, there are a few different approaches that can be taken. One approach is to use an interceptor, which can be used to add an authentication header to every request that is made. Another approach is to use a RequestInterceptor, which can be used to add an authentication header to specific requests. Finally, you can also use a Authenticator, which can be used to handle authentication errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would you recommend as the best practices for creating clean interfaces when using Retrofit?

A

I would recommend a few things when it comes to creating clean interfaces with Retrofit. First, make sure that your interface methods are well-named and clearly indicate what they are doing. Second, use annotations to clearly specify the HTTP method and endpoint for each interface method. Finally, use Retrofit’s built-in converter methods to deserialize your data into POJOs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you handle asynchronous operations when using Retrofit?

A

When using Retrofit, you can use the @retrofit annotation to specify that a method is asynchronous. This annotation will cause the Retrofit library to automatically create a new thread for the method call and will return a Call object that can be used to track the status of the operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you get access to the response body when using Retrofit?

A

When you are using Retrofit, you can get access to the response body by using the @Body annotation. This annotation will take the response body and convert it into a Java object for you to use.

17
Q

What is the role of Jackson Converter in Retrofit?

A

The Jackson Converter is responsible for converting Java objects into JSON format and vice versa. This is important because it allows Retrofit to interface with a variety of different backends.

18
Q

What is the difference between retrofit and volley in Android?

A

Retrofit and Volley are the two very popular network libraries. Though they both work in network but still there are a lot of difference in them.

Both the libraries make network calls synchronously and asynchronously but Retrofit handles request and response like simple java method calls whereas Volley has to add extra code to achieve the same.

In terms of inbuilt types Volley have only 4 response types whereas retrofit have 7 response types.

Volley has inbuilt image loading support whereas Retrofit does not provide such feature. Though other libraries like Picasso and glide can be used with Retrofit.

Volley has flexible caching mechanism whereas Retrofit does not provide support for caching.

Volley support retrying mechanism whereas Retrofit does not provide support for retrying.

For post requests and multipart uploads retrofit has full support whereas in volley the developer has to write extra code to add that feature.

Volley-
No automatic parsing
Caching Mechanism
Retry policy
inbuilt image loading support

Retrofit-
Automatic JSON parsing
No Caching mechanism
no retry policy
no Image loading