Reputation: 43
I have a typical Retrofit API request. I have used a vb.net API call and it returns the response as bellow.
<string xmlns="http://tempuri.org/"> {"UserTickets":[{"Type":"Application","Category":"Mobile App","Sub Category":"Other","Issue Details":"Dummy","LMDT":"2019-11-14 15:46:00.000","ResponsibleEmail":"[email protected]"}],"success":"true","status:":"1"}</string>
I can't able to get a response body using the below code. How can I fetch a response-body from the response object? May anyone help me?. API Call Android Code as bellow:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
CryptocurrencyService scalarService = mRetrofit.create(CryptocurrencyService.class);
Call<String> stringCall = scalarService.getStringResponse("800028");
stringCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseString = response.body().toString();
// todo: do something with the response string
Log.e("TAG_RESPONSE", responseString);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
Upvotes: 2
Views: 326
Reputation: 1149
The problem is that your response is not JSON, that's why you are not able to get the response.
Instead, if your response is a plain string you should add a Retrofit scalar converter com.squareup.retrofit2:converter-scalars:2.5.0
, you can see an example here.
Or if you want to handle the response as a XML you should add this converter compile 'com.squareup.retrofit2:converter-jaxb:2.4.0
Upvotes: 3