How to post JSON but get XML response using Retrofit2? - android

From server send Json, but Retrofit2 response is xml.
I've found a solution in this link.
retrofit solution
But this solution is retroft not retrofit2.

Use POJO Generator Plugin for custom converter response from JSON or XML, with it you can post JSON with XML response
and usage :
.setConverter(new MixedConverter(new SimpleXMLConverter(), new GsonConverter(gson)));
and use as in this

Use a custom converter with both JSON and XML converters as in this answer of the question that you referenced in the question. Then use that converter to create Retrofit instance.
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(new MixedConverter(new SimpleXMLConverter(), GsonConverterFactory.create()))
.build();
ApiService apiService = retrofit.create(ApiService.class);

Related

Handling both Json and XML response in Retrofit - Android using addConverterFactory

I am expecting both json and xml response in my app. Retrofit 2.0 allows you to add multiple Converter Factories for such situations.
But it seems the order is of utmost importance here. Adding JacksonConverterFactory above SimpleXmlConverterFactory makes Retrofit accept only Json response and throws exception when it encounters XML and vice versa.
Below is a code snippet of how to add multiple addConverterFactory to your Retrofit Builder.
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.addConverterFactory(SimpleXmlConverterFactory.create())
<<< edit
Changed the above code to this, but still not working:
return new Retrofit.Builder()
.client(clientBuilder.build())
.baseUrl(BuildConfig.API_ENDPOINT)
.addCallAdapterFactory(unAuthorizedHandlingCallAdapterFactory)
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
.addConverterFactory(new QualifiedTypeConverterFactory(JacksonConverterFactory.create(objectMapper), SimpleXmlConverterFactory.create()))
.build();
edit2
Adding the response type was the key #GET("/") #Xml
You can combine multiple Converter Factories into a single Converter Factory, check this example from retrofit samples.

Android Retrofit GsonConverterFactory

I encode data on response .
put the key on header.
Now I can decode on Observer when I get string data.
But I would like to do on GsonConverterFactory
then I can just decode on gsonConverterFactory and use gson data when response
I would like to know how to get header in GsonConverterFactory responseBodyConverter?
you need to add okhttpclient to your retrofit client for getting header
please refer this link

send the object containg list of same type of objects with to server using retrofit 2 POST

How do I send the following object to server using retrofit 2:
{"list":[
{
"addrress1":
{"addressLine1":"EktaColony",
"addressLine2":"Warje",
"country":"India",
"state":"Maharashtra",
"city":"Pune",
"zipcode":411058},
},
{address2:{.....,.....,...}}
]}
I am using Rxjava.
You can send the json as body of HTTP request. Retrofit provides the annotation #Body for its use
So in your interface
#POST("/yourserver/api")
Observable<ResponseType> sendReq(#Body RequestParser parser);
Your RequestParser Object is the object mapped from the json string. You can use any Json serializer libraries like gson or jackson for it.

Passing dynamic value to url using retrofit

I am trying to make a simple weather app using retrofit library. I want the city name to be dynamic. This is what i have done so far:
full url:
http://api.openweathermap.org/data/2.5/forecast/daily?q=dhaka&cnt=7&appid=1111
base url:
http://api.openweathermap.org/
in the main activity
LinkedHashMap<String,String>data=new LinkedHashMap<>();
data.put("q",targetCity);
data.put("cnt",Integer.toString(7));
data.put("appid",getString(R.string.api_key));
Call<WeatherResponse>weatherResponseCall=weatherServiceApi.getAllWeatherReport(data);
and in my api interface:
#GET("data/2.5/forecast/daily?")
Call<WeatherResponse>getAllWeatherReport(#QueryMap LinkedHashMap<String,String>data);
I am neither getting any error, nor any response data.
Please help.
You aren't executing the call function...
WeatherResponse response = call.execute().body();
If I were you, I would try using a ResponseBody from okhttp to get the data returned. Then using response.string() you can see what's retrieved from the request.
Or, to log the body data of each request/response you should add this interceptor when building your Retrofit object:
OkHttpClient client = httpClient.addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);

How to handle dots in json key fields, while using retrofit?

I am just simply trying to use retrofit to perform my rest api calls. The issue that I am facing is that when parsing the json, some of the key fields contain dots. For example:
{ "data": { "name.first": "first name"} }
Is it possible to configure Retrofit (or GsonConverter) to be able to handle this and how do I go about doing so?
This is neither Retrofit nor the GsonConverter's responsibility but rather Gson which sits underneath doing the actual JSON (de)serialization.
You can use Gson's #SerializedName annotation to work around names which cannot be represented in Java:
#SerializedName("name.first")
public final String firstName;
if you are using Moshi as your JSON convertor, replace it with GSON convertor factory.
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create()) //add this
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(
getOkHttpClient(
NetworkModule.networkModule.context,
enableNetworkInterceptor(baseUrl)
)
)

Categories

Resources