Android Retrofit Design Patterns

I usually use singleton pattern with following structure : first define ServiceHelper like following : public class ServiceHelper { private static final String ENDPOINT = “http://test.com”; private static OkHttpClient httpClient = new OkHttpClient(); private static ServiceHelper instance = new ServiceHelper(); private IPlusService service; private ServiceHelper() { Retrofit retrofit = createAdapter().build(); service = retrofit.create(IPlusService.class); } public … Read more

Chaining requests in Retrofit + RxJava

I don’t think using map operator is the best way to go with things like storing the result of the api call. What I like to do is to separate those things inside doOnNext operators. So your example would be something like this: apiService.A() .doOnNext(modelA -> db.store(modelA)) .flatMap(modelA -> apiService.B()) .doOnNext(modelB -> db.store(modelB)); (add necessary … Read more

How can I return String or JSONObject from asynchronous callback using Retrofit?

I figured it out. It’s embarrassing but it was very simple… Temporary solution may be like this: public void success(Response response, Response ignored) { TypedInput body = response.getBody(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(body.in())); StringBuilder out = new StringBuilder(); String newLine = System.getProperty(“line.separator”); String line; while ((line = reader.readLine()) != null) { out.append(line); … Read more

Retrofit 2 with only form-data

Here’s another Solution using request body: RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart(“param1”, param1) .addFormDataPart(“param2”, param2) .build(); apiInterface.somePostMethod(requestBody).enqueue( //onResponse onFailure methods ); here’s my api inteface POST method @POST(“somePostMethod”) Call<ResponseBody> somePostMethod(@Body RequestBody body); Hope it helps.

Android pre-lollipop devices giving error “SSL handshake aborted: ssl=0x618d9c18: I/O error during system call, Connection reset by peer”

Finally found a solution to this issue, its not a complete solution as it is a hack mentioned by Jesse Wilson from okhttp, square here. As i mentioned it was a simple hack where i had to rename my SSLSocketFactory variable to private SSLSocketFactory delegate; notice that it would throw error if you give any … Read more

Retrofit Uploading multiple images to a single key

We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution WebServicesAPI @Multipart @POST(WebServices.UPLOAD_SURVEY) Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage, @Part MultipartBody.Part propertyImage, @Part(“DRA”) RequestBody dra); Here is the method for uploading the files. private void requestUploadSurvey () { File propertyImageFile = new File(surveyModel.getPropertyImagePath()); RequestBody propertyImage = RequestBody.create(MediaType.parse(“image/*”), propertyImageFile); … Read more

Retrofit2.0 gets MalformedJsonException while the json seems correct?

Finally I solved my problem which is not related to the json lenient mode, something wrong with my POST response (there some other non json output before the json data). Here is the response from JakeWharton regarding how to set Gson lenient mode: make sure that you haveļ¼šcompile ‘com.google.code.gson:gson:2.6.1’ Gson gson = new GsonBuilder() .setLenient() … Read more

How to get string response from Retrofit2?

Add Retrofit2 and ScalarsConverterFactory to your Retrofit.Builder. adapterBuilder = new Retrofit.Builder() .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); To use ScalarsCoverter add following dependency to your build graddle implementation ‘com.squareup.retrofit2:converter-scalars:2.9.0’ implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ //Adding Retrofit2 For API Call use: Call <String> ***** Android Code : .enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.i(“Response”, response.body().toString()); //Toast.makeText() if (response.isSuccessful()){ … Read more