retrofit2

Retrofit 2 Multipart POST request sends extra quotes to PHP

烈酒焚心 提交于 2019-12-03 06:00:18
Using Retrofit 2.0.1, there is a call function in my API interface defined in Android App: @Multipart @POST("api.php") Call<ResponseBody> doAPI( @Part("lang") String lang, @Part("file\"; filename=\"image.jpg") RequestBody file ); I send the request like this: Call call = service.doAPI("eng", imageFile); where imageFile is a RequestBody created with a File object. The upload image part has no problem, while the @Part("lang") String lang part got extra quotes in server. In PHP side, it is written as follow: $lang = trim($_POST['lang']); which returns "eng" . Why there is an extra double quote

Detect if OkHttp response comes from cache (with Retrofit)

天大地大妈咪最大 提交于 2019-12-03 05:20:44
Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response? Client definition: Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(cache) .build(); Api definition: @GET("/object") Observable<Result<SomeObject>> getSomeObject(); Example call: RetroApi retroApi = new Retrofit.Builder() .client(okHttpClient) .baseUrl(baseUrl) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(RetroApi.class);

How to set timeout in Retrofit-2.0+ android

自作多情 提交于 2019-12-03 04:43:42
问题 I referred this link but I can't seem to implement for mine I am using compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' I am using the below code, How to set timeout for this ! public class ApiClient { public static final String BASE_URL = Constants.BaseURL; private static Retrofit retrofit = null; public static Retrofit getClient() { if (retrofit==null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory

How to get the request url in retrofit 2.0 with rxjava?

ⅰ亾dé卋堺 提交于 2019-12-03 04:36:05
I'm trying to upgrade to Retrofit 2.0 and add RxJava in my android project. I'm making an api call and want to retrieve the url and it with the response data in sqlite as a cache Observable<MyResponseObject> apiCall(@Body body); And in the RxJava call: myRetrofitObject.apiCall(body).subscribe(new Subscriber<MyResponseObject>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(MyResponseObject myResponseObject) { } }); In Retrofit 1.9, we could get the url in the success callback: @Override public void success(MyResponseObject

Upload file in Retrofit 2

旧时模样 提交于 2019-12-03 03:01:20
I tried the following but on response i am getting 500 error (Internal Server Error) -- help me design the interface for the request in the above screenshot...thanks @Multipart @POST("myrecord") Call<ResponseBody> addRecord(@Query("token") String token,@Query("userid") int userId, @Query("name") String name, @Part("file") RequestBody file); File file = new File(getRealPathFromURI(data.getData())); RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), getRealPathFromURI(data.getData()));` Call<ResponseBody> responseBodyCall = service.addRecord(token, userId, "newFileName",

Retrofit2 Authorisation with Bearer Token

孤街浪徒 提交于 2019-12-03 02:57:34
问题 I'm trying to use Retrofit2 , I want to add Token to my Header Like this: Authorization: Bearer Token but the code below doesn't work: public interface APIService { @Headers({"Authorization", "Bearer "+ token}) @GET("api/Profiles/GetProfile?id={id}") Call<UserProfile> getUser(@Path("id") String id); } My server is asp.net webApi . Please help what should I do? 回答1: You have two choices -- you can add it as a parameter to your call -- @GET("api/Profiles/GetProfile?id={id}") Call<UserProfile>

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

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:49: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, refresh the token and then clone.enqueue the call. There are of course several other global behaviors to

Android with Retrofit2 OkHttp3 - Multipart POST Error

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Retrofit2 with OkHttp on Android for HTTP request. Here I am doing a POST request with document upload. I ran into error below: D/OkHttp: <-- 500 Server Error http://api.drivewealth.io/v1/documents (4289ms) D/OkHttp: Date: Tue, 11 Apr 2017 03:29:48 GMT D/OkHttp: Cache-Control: must-revalidate,no-cache,no-store D/OkHttp: Content-Type: text/html; charset=ISO-8859-1 D/OkHttp: Server: Jetty(9.2.17.v20160517) D/OkHttp: Content-Length: 9323 D/OkHttp: Connection: keep-alive D/OkHttp: <html> D/OkHttp: <head> D/OkHttp: <meta http-equiv=

Android Retrofit2 Refresh Oauth 2 Token

老子叫甜甜 提交于 2019-12-03 01:58:36
问题 I am using Retrofit and OkHttp libraries. So I have Authenticator which authanticate user if gets 401 response. My build.gradle is like that : compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.okhttp3:okhttp:3.1.2' And my custom Authenticator is here : import java.io.IOException; import okhttp3.Authenticator; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; public class

How to resolve Duplicate files copied in APK META-INF/rxjava.properties

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using rxjava and rxvolley on my android aplication. When I try to run it I get this error Execution failed for task ':testapp:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties File1: C:\Users\Daniel\.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava\1.1.0\748f0546d5c3c27f1aef07270ffea0c45f0c42a4\rxjava-1.1.0.jar File2: C:\Users\Daniel\.gradle\caches\modules-2\files-2.1\io