retrofit2

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

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:56:22
问题 I can\'t find relevant methods in the Retrofit API for logging complete request/response bodies. I was expecting some help in the Profiler (but it only offers meta-data about response). I tried setting the log level in the Builder, but this doesn\'t help me either : RestAdapter adapter = (new RestAdapter.Builder()). setEndpoint(baseUrl). setRequestInterceptor(interceptor). setProfiler(profiler). setClient(client). setExecutors(MyApplication.getWebServiceThreadPool()). setLogLevel(LogLevel

Send file to server via retrofit2 as object

十年热恋 提交于 2019-11-26 11:38:26
问题 I want to send an audio file to a server with retrofit2 . I followed this tutorial but the file is not in the format the server accepts. Based on this tutorial I tried the following: RequestBody requestBody = RequestBody.create(MediaType.parse(\"multipart/form-data\"), file); MultipartBody.Part audio = MultipartBody.Part.createFormData(\"file\", \"file\", requestBody); and the interface: @Headers(\"Content-Type: application/json\") @Multipart @POST(\"app/\") Call<JResponse> upload(@Part(\

How to Upload Image file in Retrofit 2

若如初见. 提交于 2019-11-26 11:22:13
I have an image of postman like below. How to do the same thing in Retrofit 2. I've declared the interface like this. @Multipart @POST("/api/Pharmarcy/UploadImage") Call<ResponseBody> uploadPrescriptionImage( @Query("accessToken") String token, @Query("pharmarcyRequestId") int pharmacyRequestedId, @Part MultipartBody.Part image); android_griezmann @Multipart @POST("user/updateprofile") Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id, @Part("full_name") RequestBody fullName, @Part MultipartBody.Part image, @Part("other") RequestBody other); //pass it like this File file =

Android Retrofit Design Patterns

谁说我不能喝 提交于 2019-11-26 11:19:25
问题 I am using Retrofit to interact with my REST API, and was wondering whether anybody has any design suggestions. My app has the following packages: models services activities fragments The services package contains the interfaces for Retrofit. For example: public interface FooService { @FormUrlEncoded @POST(\"foo/do\") @Headers(\"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\") Call<FooBar> do(); } Models contains...well, the different models. For example, FooBar . So far so

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

烂漫一生 提交于 2019-11-26 11:14:25
问题 Iam having this strange issue in which the retrofit keeps throwing me \"SSL handshake aborted: ssl=0x618d9c18: I/O error during system call, Connection reset by peer\" in kitkat, whereas the same code working fine in lollipop devices. Iam using an OkHttpClient client like the following public OkHttpClient getUnsafeOkHttpClient() { try { final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted( java.security.cert

Parse JSON array response using Retrofit & Gson

无人久伴 提交于 2019-11-26 11:09:30
问题 Here is my JSONArray Response from Web service: [ { \"sponsors\": [ { \"leg_id\": \"NYL000067\", \"type\": \"primary\", \"name\": \"AUBRY\" }, { \"leg_id\": \"NYL000171\", \"type\": \"cosponsor\", \"name\": \"PERRY\" }, { \"leg_id\": \"NYL000066\", \"type\": \"cosponsor\", \"name\": \"ARROYO\" }, { \"leg_id\": \"NYL000223\", \"type\": \"cosponsor\", \"name\": \"BARRETT\" }, { \"leg_id\": \"NYL000312\", \"type\": \"cosponsor\", \"name\": \"STECK\" }, { \"leg_id\": \"NYL000180\", \"type\": \

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

痴心易碎 提交于 2019-11-26 10:30:52
问题 For example, calling api.getUserName(userId, new Callback<String>() {...}); cause: retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 I think I must disable gson parsing into POJOs but can\'t figure out how to do it. 回答1: I figured it out. It's embarrassing but it was very simple... Temporary solution may be like this: public void success(Response

How to get Retrofit success response status codes

若如初见. 提交于 2019-11-26 08:17:29
问题 I am not able to get success response status code from response like 200,201.. etc. As we can easily get error codes from RetrofitError class like error.isNetworkError() and error.getResponse().getStatus() . Is there any workaround for getting status codes? 回答1: As per Retrofit 2.0.2, the call is now @Override public void onResponse(Call<YourModel> call, Response<YourModel> response) { if (response.code() == 200) { // Do awesome stuff } else { // Handle other response codes } } Hope it helps

Retrofit - Multipart request: Required MultipartFile parameter &#39;file&#39; is not present

一世执手 提交于 2019-11-26 05:35:02
问题 I\'m trying to send file on server using Retrofit2. I do everything according documentation, but always get 400 server error. I\'m tried to do like this: RequestBody body = RequestBody.create(MediaType.parse(\"image/png\"), photo); //.......... @Multipart @POST(ADD_PHOTO) Observable<HPSPhotoResponse> addPhoto(@Part(\"file\") RequestBody file); ...and like this: MultipartBody.Part part = MultipartBody.Part.createFormData(\"file\", \"file\", body); //........... @Multipart @POST(ADD_PHOTO)

Retrofit2 Android: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

只愿长相守 提交于 2019-11-26 03:10:49
问题 I know this is not the first time someone asking about this problem but with Retrofit2 I can\'t find the right solution to my problem. I followed a online tutorial and it worked just fine. When I applied same code to my own endpoint i get this exception: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ I don\'t know how to solve this. Interface: public interface MyApiService { // Is this right place to add these headers? @Headers({\