retrofit2

RxJava + Retrofit -> BaseObservable for API calls for centralized response handling

做~自己de王妃 提交于 2019-12-03 11:16:55
问题 I am new to RxJava so please forgive me if this sounds too newbie :-). As of now I have an abstract CallbackClass that implements the Retofit Callback. There I catch the Callback's "onResponse" and "onError" methods and handle various error types before finally forwarding to the custom implemented methods. I also use this centralized class to for request/response app logging and other stuff. For example: for specific error codes from my sever I receive a new Auth token in the response body,

Retrofit error-Missing either @GET URL or @Url parameter

风格不统一 提交于 2019-12-03 10:32:43
问题 I am working on Youtube API. The base URL is <https://www.googleapis.com/youtube/v3/search/> Request :GET https://www.googleapis.com/youtube/v3/search?part=snippet&q={search_keyword}&key={API_KEY} ApiService Interface code- public interface ApiService { @GET("") Call<YoutubeResponse> searchVideos(@Query("part") String part, @Query("q") String q,@Query("key") String apiKey); } The error: java.lang.IllegalArgumentException: Missing either @GET URL or @Url parameter. in the line of code Call

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

醉酒当歌 提交于 2019-12-03 10:32:16
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 { Response response = chain.proceed(chain.request()); // re-write response header to force use of cache

Retrofit “IllegalStateException: Already executed”

南笙酒味 提交于 2019-12-03 09:41:59
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 once, but then throws the following: java.lang.IllegalStateException: Already executed. at retrofit2

Android Retrofit 2 + RxJava: listen to endless stream

吃可爱长大的小学妹 提交于 2019-12-03 09:08:27
问题 Can I use Retrofit + RxJava to listen to an endless stream? For instance the Twitter stream. What I have is this: public interface MeetupAPI { @GET("http://stream.meetup.com/2/rsvps/") Observable<RSVP> getRSVPs(); } MeetupAPI api = new Retrofit.Builder() .baseUrl(MeetupAPI.RSVP_API) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(MeetupAPI.class); api.getRSVPs() .subscribeOn(Schedulers.newThread()) .observeOn

Retrofit-2 Content-Type Issue

孤者浪人 提交于 2019-12-03 08:33:28
问题 My Api is accepting Content-Type application/json as headers. I set Header perfectly as mentioned in Retrofit Docs. @Headers("Content-Type: application/json") @POST("user/classes") Call<playlist> addToPlaylist(@Body PlaylistParm parm); But in Request Log it is Returning Content-Type txt/html.So how i should fix this issue? This api works fine in POSTMAN 回答1: you can try one in below my solutions : @POST("user/classes") Call<playlist> addToPlaylist(@Header("Content-Type") String content_type,

Unit Testing in Retrofit for Callback

孤人 提交于 2019-12-03 07:44:26
问题 Here i got a sample of code in presenter. How do i make write a test for onSuccess and onFailure in retrofit call public void getNotifications(final List<HashMap<String,Object>> notifications){ if (!"".equalsIgnoreCase(userDB.getValueFromSqlite("email",1))) { UserNotifications userNotifications = new UserNotifications(userDB.getValueFromSqlite("email",1),Integer.parseInt(userDB.getValueFromSqlite("userId",1).trim())); Call call = apiInterface.getNotifications(userNotifications); call.enqueue

InterruptedIOException while using Retrofit2 with rx when retryOn

↘锁芯ラ 提交于 2019-12-03 07:24:19
I'm using retrofit 2 with rx-android I have quite complex retry logic, which partially works . I cut off irrelevant code to simplify it. Here it is: public class RetryWithDelay implements Func1<Observable<? extends Throwable>, Observable<?>>, Constants { @Override public Observable<?> call(Observable<? extends Throwable> attempts) { return attempts.flatMap(new Func1<Throwable, Observable<?>>() { @Override public Observable<?> call(Throwable throwable) { int statusCode = 500; if (throwable instanceof HttpException) { HttpException httpException = (HttpException) throwable; statusCode =

Unexpected end of stream on okhttp3

大城市里の小女人 提交于 2019-12-03 07:05:56
I had followed this link to refresh access token. While adding Authenticator to okHttp getting error of Unexpected end of stream on okhttp3 from retrofit call back onFailure method public class TokenAuthenticator implements Authenticator { AccessTokenRefreshModel accessTokenRefreshModel = null; @Override public Request authenticate(Route route, Response response) throws IOException { Call<UserLogin> call = iService.refreshAccessToken(BuildConfig.CLIENT_ID, refreshToken); UserLogin userLogin = call.execute().body(); // Add new header to rejected request and retry it return response.request()

java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454

假装没事ソ 提交于 2019-12-03 06:10:51
I'm using the retrofit library for my calls in a demo project. I received the following error: java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454 path $.result.results.ads[2].acres I under stand that this is down to GSON. I will show you the JSON it's getting caught in: { "ad_id":739580087654, "property_type":"site", "house_type":"", "selling_type":"private-treaty", "price_type":"", "agreed":0, "priority":2, "description":"Beautiful elevated 0.6 acre site - zoned residential - and within easy walk to this popular and scenic coastal village\r\n\r\n\r\nthe site