retrofit2

Retrofit Android basic and simple issue

不问归期 提交于 2019-12-06 13:13:05
My server returns simple Json result like below, {"message":"Upload Success.!"} I am trying to get the result into Retrofit Model class public class MyResponse { @SerializedName("message") String message; } My interface class is here public interface MyService { @Multipart @POST("/") public retrofit2.Call<MyResponse> saveFile(@Part("filename") String fileName, @Part("photo") RequestBody photo); } this is how i try to fetch the result Gson gson = new GsonBuilder() .setLenient() .create(); Retrofit retrofit = new Retrofit.Builder().baseUrl("http://xxxxxxx.com/fileupload.php/")

OKHttp Authenticator custom http code other than 401 and 407

萝らか妹 提交于 2019-12-06 12:49:28
问题 I have oauth token implemented on server side but upon Invalid token or Token expirey i am getting 200 http status code but in response body i have {"code":"4XX", "data":{"some":"object"} When i try to read string in interceptor i get okhttp dispatcher java.lang.illegalstateexception closed because response.body().string() must be called only once. Also i read from here Refreshing OAuth token using Retrofit without modifying all calls that we can use OkHttp Authenticator class but it works

Android: Can not construct instance of java.sql.Timestamp from String value

六月ゝ 毕业季﹏ 提交于 2019-12-06 11:37:18
I have developed a REST API and I am trying to connect to it using Android. Below is my code. private void restCall() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(URL) .addConverterFactory(GsonConverterFactory.create()) .build(); YourEndpoints request = retrofit.create(YourEndpoints.class); SetupBean setupBean = new SetupBean(); setupBean.setIdPatient(1); setupBean.setCircleType(1); setupBean.setFamiliarity(1); setupBean.setValence(2); setupBean.setArousal(3); setupBean.setDateCreated(Common.getSQLCurrentTimeStamp()); setupBean.setLastUpdated(Common.getSQLCurrentTimeStamp()); Call

Custom rx Func1 for retrofit response code handling

眉间皱痕 提交于 2019-12-06 11:10:50
问题 I am new in rxjava, so please don't be strict... I have request lice next one: Observable<Login>login(String l, String p){ return api.loginUser(l,p) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Func1<Response<Login>, Observable<? extends Login>>() { @Override public Observable<? extends Login> call(Response<Login> r) { switch (r.code()){ case 200: requestOk(r); break; case 202: //need to Repeat login.timeout(2000,TimeUnit.Milliseconds); break; default:

Custom HTTP method not working in Retrofit 2

空扰寡人 提交于 2019-12-06 10:49:06
问题 Hello I have used custom method in my code as below but it always gives me java.lang.IllegalArgumentException: method AUTH must not have a request body. My code is not working it always says: Custom method AUTH, must not have a Body @Headers("Content-Type: application/json") @HTTP(method = "AUTH", path = "login/{deviceId}", hasBody = true) Call<Success> getLogin( @Path("deviceId") int deviceId, @Body RequestBody password); I am using below dependancies. compile 'com.google.code.gson:gson:2.8

How to upload Multiple images in one Request using Retrofit 2 and php as a back end?

夙愿已清 提交于 2019-12-06 10:31:04
问题 I am making an app in which user can select multiple images and upload them to the server. I am using PHP as a backend and retrofit2 I tried all answers on stackoverflow but still did not resolve it. @Multipart @POST("URL/uploadImages.php") Call<Response> uploaImages( @Part List< MultipartBody.Part> files ); code for sending files Retrofit builder = new Retrofit.Builder().baseUrl(ROOT_URL).addConverterFactory(GsonConverterFactory.create()).build(); FileUploadService fileUploadService =

How to retry a consumed Observable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 09:29:06
I am trying to re-execute a defined observable that failed. Using Retrofit2 and RxJava2 together i want to retry a specific request with its subscription and behavior when clicking a button. is that possible? service.excecuteLoginService(url, tokenModel, RetrofitManager.apiKey) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(....) An option is to create Publisher, which emission is controlled by your button. final PublishSubject<Object> retrySubject = PublishSubject.create(); service.excecuteLoginService(url, tokenModel, RetrofitManager.apiKey)

Refreshing data using SQLBrite + Retrofit

拥有回忆 提交于 2019-12-06 08:22:05
问题 Here is my use case: I am developing an app that communicates with a server via a REST API and stores the received data in a SQLite database (it's using it as a cache of some sorts). When the user opens a screen, the following has to occur: The data is loaded from the DB, if available. The app call the API to refresh the data. The result of the API call is persisted to the DB. The data is reloaded from the DB when the data change notification is intercepted. This is very similar to the case

How to add custom header to retrofit request body part?

江枫思渺然 提交于 2019-12-06 06:49:51
问题 So I am building a multipart request using retrofit and I want to add a custom header to one one of the part. I have tried a lot of things but nothing seems to be working. The photo shows a body request that contains 4 parts. Each part contains a body and a header, what I want is to add a custom header to the selected part. How can I do that? 回答1: So just to answer my own question, I create a part with an image and a custom header like this: MultipartBody.Part body = MultipartBody.Part.create

Retrofit 2 simple upload file to server

三世轮回 提交于 2019-12-06 06:08:40
i'm newbie in retrofit and in this code i want to upload file as an image or other format to server, i read Retrofit document about that, but i can't do that, i can create provider and i can use that to POST or GET from server, but my problem is for that is upload file. Thanks to advance My SignalProvider class: public class SignalProvider { private SignalRetrofitServiceProviders signalRetrofitServiceProviders; public SignalProvider(){ OkHttpClient httpClient = new OkHttpClient(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ClientSettings.SignalWebBaseUrl) .client(httpClient)