retrofit2

Retrofit 2 : send files with json object

删除回忆录丶 提交于 2019-11-30 03:07:03
问题 I'm sending object in request body, something like that : { "title":"test", "description":"test", "images":[] } @POST("create-data") Call<JsonObject> publishData(@Body MyObject object); and it's work fine without the images. From the docs I can find how to upload file to server using MultipartBody.Part , my questions is : How can I upload multiple images at the same time? Is it possible to send the images inside the object, or I need to send it separately and how ? thank you very much. 回答1:

OkHttpClient throws exception after upgrading to OkHttp3

喜夏-厌秋 提交于 2019-11-30 01:27:38
I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }); } After upgrading retrofit to beta-3 version, I had to upgrade OkHttp to OkHttp3 also (actually I just changed package names from okhttp to okhttp3, the

Retrofit API call receives “HTTP FAILED: java.io.IOException: Canceled”

*爱你&永不变心* 提交于 2019-11-30 00:21:16
问题 Can't figure out why is this happening. Neither one of rx callbacks (onCompleted(), onError(), onNext()) not gets triggered by my call. The only thing i receive is this okhttp output: D/OkHttp: --> GET https://api.privatbank.ua/p24api/exchange_rates?json=true&date=20.11.2016 http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled Retrofit module: @Module public class RestModule { @Provides @Singleton public HttpLoggingInterceptor providesHttpLogginInterceptor()

Retrofit not working on specific versions of android

血红的双手。 提交于 2019-11-29 20:43:51
问题 I have a problem with Retrofit on my emulator running Android 4.3 and my device is on Android 4.4.2 while the same code runs normally on another emulator with Android 7.1.1 Each time I try to execute the get request I get a timeout exception. java.net.SocketTimeoutException: failed to connect to jsonplaceholder.typicode.com/2606:4700:30::681c:3f5 (port 443) after 10000ms at libcore.io.IoBridge.connectErrno(IoBridge.java:159) at libcore.io.IoBridge.connect(IoBridge.java:112) at java.net

Dagger + Retrofit. Adding auth headers at runtime

不羁岁月 提交于 2019-11-29 20:28:09
I'm wondering if there is a way for Dagger to know that it should recreate an object when new data is available. The instance I am speaking of is with the request headers I have for retrofit. At some point (when the user logs in) I get a token that I need to add to the headers of retrofit to make authenticated requests. The issue is, I'm left with the same unauthenticated version of retrofit. Here's my injection code: @Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel

How to handle errors with Retrofit?

本小妞迷上赌 提交于 2019-11-29 17:45:09
My server returns a basic JSON structure for all request like: { "success": false, "data": { "errors": { "email": [ "This is not an email." ], "password": [ "The password must be at least 6 characters." ] } } } Where success can be true or false, and data can return a number of things, from errors to data the app might need. How can I handle this response (both success and error) using Retrofit? What would need to be changed/added to my Retrofit API call? Call<BasicResponse> call = apiService.login(emailString, passwordString); call.enqueue(new Callback<BasicResponse>() { @Override public void

How to parse dynamic json in android with retrofit 2 using annotations

家住魔仙堡 提交于 2019-11-29 17:30:19
I have a JSON structure which I want to parse using retrofit 2 (@Expose). Below I have mentioned the JSON. Need help to parse it using dynamic annotations. { "status": 1, "message": "success", "data" : [ { "type": 1, "heading": "", "description": "", "created_on": 141123213, "author_id": 123, "author_name": "some name", "author_pic": "some_pic", "read_time": "3.1 min", "post_pic_url": "", "post_web_url": "", "isLiked": false, "isSaved": false, "totalLikes": 12 }, { "type": 2, "author_id": 123, "author_name": "some name", "author_pic": "some pic", "author_about": "", "tags":[ "travel",

NPE error using Retrofit

半腔热情 提交于 2019-11-29 17:27:18
I want to login in some service called vid.me, https://api.vid.me/oauth/authorize with POST.But when I try to get data from log I have NullPointerException.I tryed to make Toast and have this error too.I'm trying to get response code to see I did this right or no. my API class: public interface VideoApi { @GET("/videos/featured") Call<Videos> getFeaturedVideo(); @GET("/videos/new") Call<Videos> getNewVideo(); @FormUrlEncoded @POST("oauth/authorize") Call<SignInResults>insertUser(@Field("name") String name, @Field("password") String password ); } my fragment: public class FeedFragment extends

How do I parse a field from a deep nested json object using Gson and retrofit in android?

北城以北 提交于 2019-11-29 17:21:31
I have a unique situation where I have to get certain times from a json's deeply nested object. It's a little complex, I couldn't find a solution so looking for ideas and ways to tackle this I have a the json as follows: [{ "mySpaceId": 73220, "myBuildingId": 14019, "myFloorId": 10569, "myFloorNumber": "4", "myFloorName": "4th Floor", "spaceName": "My Room 4", "capacity": 5, "type": "huddle", "busyAt": [] }, { "mySpaceId": 73219, "myBuildingId": 14019, "myFloorId": 10569, "myFloorNumber": "4", "myFloorName": "4th Floor", "spaceName": "My room 5", "description": null, "capacity": 4, "type":

Retrofit POST Method with Json data got error code 400 : Bad Request

孤街浪徒 提交于 2019-11-29 15:40:29
问题 I would like to call a POST Method(Magento REST API) in Retrofit with a JSON data(provide JSON as JsonObject). For that, I call as follow from the postman and work fine for me. I have done the android parts as follow, API INTERFACE public interface APIService { @POST("seq/restapi/checkpassword") @Headers({ "Content-Type: application/json;charset=utf-8", "Accept: application/json;charset=utf-8", "Cache-Control: max-age=640000" }) Call<Post> savePost( @Body JSONObject jsonObject );} APIUtility