Am new to android i want to parse the xml file using retrofit, i have gone through the links which are posted in stackoverflow
How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?
But in my scenario i have to add headers,how can i achieve this using Retrofit.
Asked
Active
Viewed 7,580 times
5

santosh nani
- 147
- 3
- 11
2 Answers
3
Sample web service without headers:
public interface ApiService {
@GET("/webservice/xml")
Call<YourClass> getXml();
}
This is how we add static and dynamic headers:
public interface ApiService {
@Headers({"Accept: application/json"})
@GET("/webservice/xml")
Call<YourClass> getXml(@Header("Authorization") String authorization);
}
Using ApiService:
new Retrofit.Builder()
.baseUrl("server ip")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build().create(ApiService.class).getXml("This is a value that will be sent as authorization header");
In order to use this code you should add these lines to your gradle file:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile('com.squareup.retrofit2:converter-simplexml:2.1.0') {
exclude group: 'xpp3', module: 'xpp3'
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
}
Here is a good tutorial https://futurestud.io/tutorials/retrofit-add-custom-request-header

a.toraby
- 3,232
- 5
- 41
- 73
-
can you please post the sample code by using headers, – santosh nani Oct 09 '18 at 11:36
-
can you please look into this as i am facing nullpointerexception while fetching response https://stackoverflow.com/questions/52945188/how-to-parse-xml-response-using-retrofit-2-0 – santosh nani Oct 25 '18 at 05:29
0
Example interface with header as a static value:
public interface ApiService {
@Headers("user-key: 9900a9720d31dfd5fdb4352700c")
@GET("api/v2.1/search/webxml")
Call<String> getRestaurantsBySearch(@Query("q"), String query);
}
Example interface with header as a parameter:
public interface ApiService {
@GET("api/v2.1/search/webxml")
Call<String> getRestaurantsBySearch(@Query("q"), String query, @Header("user-key") String userkey);
}

Dearwolves
- 443
- 4
- 16
-
can you please look into this as i am facing nullpointerexception while fetching response https://stackoverflow.com/questions/52945188/how-to-parse-xml-response-using-retrofit-2-0 – santosh nani Oct 25 '18 at 05:31