retrofit2

Retrofit 2 synchronous call error handling for 4xx Errors

与世无争的帅哥 提交于 2019-12-04 19:13:54
问题 I'm using a android-priority-jobqueue and I use retrofit to make synchronous calls to my rest api but i'm unsure how to handle errors like 401 Unauthorized errors which I send back json stating the error. Simple when doing async calls but I'm adapting my app for job manager. below is a simple try catch for IO exceptions, but 401's 422's etc? How to do this? try { PostService postService = ServiceGenerator.createService(PostService.class); final Call<Post> call = postService.addPost(post);

OKHttp Authenticator custom http code other than 401 and 407

大憨熊 提交于 2019-12-04 19:08:17
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 only with 401/407 i havent triedn as i will not get this. Is there any way we can customize Authenticator

Not able to load data from cache okHttp & retrofit

拥有回忆 提交于 2019-12-04 17:41:54
问题 Here is my code where i am calling api and also define cache for okhttp with retrofit: public class DemoPresenter { DemoView vDemoView; private Context mContext; public DemoPresenter(Context mcontext, DemoView vDemoView) { this.vDemoView = vDemoView; this.mContext = mcontext; } public void callAllProduct() { if (vDemoView != null) { vDemoView.showProductProgressBar(); } OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(new Cache(mContext.getCacheDir(), 10 * 1024 * 1024)) // 10 MB

Got this error with retrofit2 & OkHttp3. Unable to resolve host “<host-name>”: No address associated with hostname

我的未来我决定 提交于 2019-12-04 16:32:10
问题 I am using the retrofit 2 and OkHttp3 to request data from server. I just added a offline cache code but It's not working as expected. I got the error "Unable to resolve host "<>": No address associated with hostname." This occurs when It's try to get the retrieve data from the cache(when no internet connection). A code snippet is below. public static Interceptor provideCacheInterceptor() { return new Interceptor() { @Override public Response intercept(Chain chain) throws IOException {

Rx 2 Android what is better Single or Observable for api calls?

岁酱吖の 提交于 2019-12-04 16:25:49
问题 when we use retrofit2 for doing API rest calls with Rx, What is the best approach to use, Single or Observable? public interface ApiService{ Single<Data> getDataFromServer(); Observable<Data> getDataFromServer(); } 回答1: I'd suggest using a Single as it is more accurate representation of the data flow: you make a request to the server and the you get either one emission of data OR an error: Single: onSubscribe (onSuccess | onError)? For an Observable you could theoretically get several

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

一个人想着一个人 提交于 2019-12-04 16:05:18
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 = builder.create(FileUploadService.class); Call<Response> call = fileUploadService.uploadImages(list) for (Uri

Retrofit “IllegalStateException: Already executed”

痴心易碎 提交于 2019-12-04 15:37:26
问题 I have a Retrofit network call that id like to run every 5 seconds. My current code: Handler h = new Handler(); int delay = 5000; //milliseconds h.postDelayed(new Runnable() { public void run() { call.enqueue(new Callback<ApiResponse>() { @Override public void onResponse(Response<ApiResponse> response) { Log.d("api", "response: " + response.body().getPosition().getLatitude().toString()); } @Override public void onFailure(Throwable t) { } }); h.postDelayed(this, delay); } }, delay); This runs

Implementing Retrofit2 service interface

 ̄綄美尐妖づ 提交于 2019-12-04 15:28:13
Imagine the following scenario. I am using Retrofit as my API client and I am going to use a caching mechanism to persist some responses locally in let's say SQLite database, so some of the data is obtained only once. It seems like a perfect case to create a custom implementation for the following interface which is going to fetch the data from local database when network is not available. public interface CommentsService { @GET("posts/{postId}/comments") Call<List<Comment>> getCommentsOfPost(@Path("postId") int id); } The problem is that all service methods in retrofit2 should be wrapped in a

Custom rx Func1 for retrofit response code handling

谁说胖子不能爱 提交于 2019-12-04 15:12:08
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: // } } }); } Observable<Login> requestOk(final Response<Login> r){ return Observable.create(new

Realm and Retrofit2: sending auto-managed objects

本小妞迷上赌 提交于 2019-12-04 14:03:15
When using Realm and Retrofit2 for sending auto-managed RealmObjects to our server, Retrofit2 (using Gson) only sends the ints in the RealmObject. It completely ignores the Strings and other fields, and does not put these in the json. No errors are logged. If I however, disconnect the RealmObject from Realm: realm.copyFromRealm(myRealmObject) then it does send all fields. What could be the problem? Is there a proper solution? Before we dive in In one of my posts here on Stackoverflow, I have explained what's happening when using Gson and Realm together (Retrofit is just using Gson as a data