retrofit2

how to use retrofit 2 to send file and other params together

天涯浪子 提交于 2019-12-04 13:42:31
I am looking for an example how could I send file and other params together to server. I have to send server JSON which { "title": "title", "file": "uploaded file instance", "location": { "lat": 48.8583, "lng": 2.29232, "place": "Eiffel Tower" } } How could I create Retrofit to handle this case? If file is a string I know how to handle this. If file is File object I have no idea how to do this. Use gson and create a model class for the location. Add the following dependencies to your build.gradle . compile 'com.squareup.retrofit2:converter-gson:2.0.0' compile 'com.google.code.gson:gson:2.5'

Retrofit OKHTTP Offline caching not working

梦想的初衷 提交于 2019-12-04 13:38:39
问题 I read dozens of tutorial and Stackoverflow answers to my problem but nothing is working for me! Also, most of them are old so probably OKHTTP changed somehow. All I want is to enable offline caching for Retrofit. I am using GET I tried using only offlineCacheInterceptor as an Interceptor, but I kept getting: Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname I tried using a combination of offlineCacheInterceptor as an Interceptor +

Retrofit 2.2.0 Android API 24 javax.net.ssl.SSLHandshakeException: Handshake failed

柔情痞子 提交于 2019-12-04 13:07:28
I'm using Retrofit 2.2.0 for uploading image to server (using Java). With an Android device (Samsung galaxy S6) API 24 (Build : NRD90M.G920FXXU5EQAC) when I try to post a request, this request failed with this error javax.net.ssl.SSLHandshakeException: Handshake failed ps: I try to downgrade the Retrofit 2.1.0 and it works perfectly. The solution for me was adding more ciphers as acceptable for OkHttpClient. Since API 21, some TLS certificates are deprecated for Android. This might help: ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2

Refreshing data using SQLBrite + Retrofit

匆匆过客 提交于 2019-12-04 13:05:39
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 presented here , but there is a slight difference. Since I am using SQLBrite, the DB observables don't

Enable TLS 1.2 in Android 4.4

拜拜、爱过 提交于 2019-12-04 12:17:43
问题 I use Retrofit and OkHttp3 for making requests. I konw that in Android 4.4 TLS 1.1 and TLS 1.2 are not enabled by defult. So i'm trying to enable them. But so far i had no sucsess. I read that it could be a problem of the android studio emulator, but i can't make a test on a real device with andoroid 4.4 rigthnow This is what i have done so far: private <S> S createService(Class<S> serviceClass) { Retrofit retrofit = builder.client(getNewHttpClient()).build(); return retrofit.create

Retrofit 2 Multipart POST request sends extra quotes to PHP

随声附和 提交于 2019-12-04 10:31:59
问题 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

Retrofit uploading a picture

风格不统一 提交于 2019-12-04 10:24:44
I'm using Retrofit v.2.2.0 to upload a picture to my server but the server returns a null value stating no image has been uploaded. The logs shows the image is uploaded and the file name is correct when uploaded. What can be the problem yet it works in postman? UploadProfilePicture.java public class UploadProfilePicture extends ContextWrapper { private GetOnUpload onUpload; private File imageFile,filesDir,file; private Uri fileUri; private String profilePic = "profile_pic"; private String fileName; private ParcelFileDescriptor parcelFileDescriptor; private FileDescriptor fileDescriptor;

How to handle different kinds of errors in Retrofit Rx onError without ugly instanceof

拈花ヽ惹草 提交于 2019-12-04 08:42:32
问题 I would like to know your ways to handle different kinds of errors (like http exceptions, no internet connection exceptions etc) in retrofit Rx onError without using instanceof like it's proposed here: How to handle network errors in Retrofit 2 with RxJava or here: Handle errors in Retrofit 2 RX In kotlin I will simply make some extension functions for each kind of throwable to do whatever I want. But I am forced to use Java in the project. Any nice suggestions? is the approach to build some

Android - Proguard and retrofit 2?

橙三吉。 提交于 2019-12-04 08:39:09
问题 I am using from Proguard for my project and bellow code is in my proguard-rules.pro : # Retrofit -keep class com.google.gson.** { *; } -keep class com.google.inject.** { *; } -keep class org.apache.http.** { *; } -keep class org.apache.james.mime4j.** { *; } -keep class javax.inject.** { *; } -keep class javax.xml.stream.** { *; } -keep class retrofit.** { *; } -keep class com.google.appengine.** { *; } -keepattributes *Annotation* -keepattributes Signature -dontwarn com.squareup.okhttp.*

Send authorization header with every request in webview using okhttp in android

我的梦境 提交于 2019-12-04 08:03:07
I am using WebView to display web page, but the server expects an authorization token with every request from my webview. Anybody know if this is possible ? I referred this post in #SO adding-header-to-all-request-with-retrofit-2 . But I'm not able to get the result. This is my code (bare with my coding standard, I'm a beginner) public class TableViewTest extends AppCompatActivity { ScrollView scrollView; WebView webView; SharedPreferences pref; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);