Retrofit 2 – URL Query Parameter

If you specify @GET(“foobar?a=5”), then any @Query(“b”) must be appended using &, producing something like foobar?a=5&b=7. If you specify @GET(“foobar”), then the first @Query must be appended using ?, producing something like foobar?b=7. That’s how Retrofit works. When you specify @GET(“foobar?”), Retrofit thinks you already gave some query parameter, and appends more query parameters using … Read more

retrofit 2 @path Vs @query

Consider this is the url: www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School Now this is the call: @GET(“/api/searchtypes/{Id}/filters”) Call<FilterResponse> getFilterList( @Path(“Id”) long customerId, @Query(“Type”) String responseType, @Query(“SearchText”) String searchText ); So we have: www.app.net/api/searchtypes/{Path}/filters?Type={Query}&SearchText={Query} Things that come after the ? are usually queries.

Unable to create call adapter for io.reactivex.Observable

You need to tell Retrofit that you want to use RxJava 2, using: addCallAdapterFactory(RxJava2CallAdapterFactory.create()) So, for creating your Retrofit object, you will have something like: Retrofit retrofit = new Retrofit.Builder() .baseUrl(SERVICE_ENDPOINT) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build();

How to log request and response body with Retrofit-Android?

Retrofit 2.0 : UPDATE: @by Marcus Pöhls Logging In Retrofit 2 Retrofit 2 completely relies on OkHttp for any network operation. Since OkHttp is a peer dependency of Retrofit 2, you won’t need to add an additional dependency once Retrofit 2 is released as a stable release. OkHttp 2.6.0 ships with a logging interceptor as … Read more

How can I handle empty response body with Retrofit 2?

Edit: As Jake Wharton points out, @GET(“/path/to/get”) Call<Void> getMyData(/* your args here */); is the best way to go versus my original response — You can just return a ResponseBody, which will bypass parsing the response. @GET(“/path/to/get”) Call<ResponseBody> getMyData(/* your args here */); Then in your call, Call<ResponseBody> dataCall = myApi.getMyData(); dataCall.enqueue(new Callback<ResponseBody>() { @Override … Read more

POST Multipart Form Data using Retrofit 2.0 including image

There is a correct way of uploading a file with its name with Retrofit 2, without any hack: Define API interface: @Multipart @POST(“uploadAttachment”) Call<MyResponse> uploadAttachment(@Part MultipartBody.Part filePart); // You can add other parameters too Upload file like this: File file = // initialize file here MultipartBody.Part filePart = MultipartBody.Part.createFormData(“file”, file.getName(), RequestBody.create(MediaType.parse(“image/*”), file)); Call<MyResponse> call = … Read more

Retrofit 2.0 how to get deserialised error response.body

I currently use a very easy implementation, which does not require to use converters or special classes. The code I use is the following: public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { DialogHelper.dismiss(); if (response.isSuccessful()) { // Do your success stuff… } else { try { JSONObject jObjError = new JSONObject(response.errorBody().string()); Toast.makeText(getContext(), jObjError.getJSONObject(“error”).getString(“message”), Toast.LENGTH_LONG).show(); } catch … Read more

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

Reason: This error occurs because jackson library doesn’t know how to create your model which doesn’t have an empty constructor and the model contains constructor with parameters which didn’t annotated its parameters with @JsonProperty(“field_name”). By default java compiler creates empty constructor if you didn’t add constructor to your class. Solution: Add an empty constructor to … Read more

Logging with Retrofit 2

In Retrofit 2 you should use HttpLoggingInterceptor. Add dependency to build.gradle. Latest version as of October 2019 is: implementation ‘com.squareup.okhttp3:logging-interceptor:4.2.1’ Create a Retrofit object like the following: HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(“https://backend.example.com”) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(ApiClient.class); In case of deprecation warnings, simply … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)