Skip to content

Instantly share code, notes, and snippets.

@c3stream
Last active October 3, 2017 17:40
Show Gist options
  • Select an option

  • Save c3stream/45b2abd901744bc14c0833295ea1220b to your computer and use it in GitHub Desktop.

Select an option

Save c3stream/45b2abd901744bc14c0833295ea1220b to your computer and use it in GitHub Desktop.
RetrofitBug
import android.util.Log
import okhttp3.ResponseBody
import retrofit2.Retrofit
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query
class RetrofitBug {
val server = MockWebServer()
internal interface Service {
@GET("/path")
@Headers("User-Agent: Test Case")
fun search(@Query("keyword") keyword: String): Call<ResponseBody>
}
fun test() {
Thread(Runnable {
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.build()
val example = retrofit.create(Service::class.java)
server.enqueue(MockResponse()
.addHeader("Content-Type", "text/plain")
.setBody("Hi"))
val call = example.search("hello")
val response = call.execute()
Log.e("text/plain", response.headers().get("Content-Type"))
Log.e("Hi", response.body()?.string())
val recordedRequest = server.takeRequest()
Log.e("/path?keyword=hello", recordedRequest.getPath())
Log.e("Test Case", recordedRequest.getHeader("User-Agent"))
}).start()
}
}
@c3stream
Copy link
Copy Markdown
Author

c3stream commented Sep 28, 2017

Result

/text/plain: text/plain
/Hi: Hi
/path?keyword=hello: /path
/Test Case: Test Case

Retrofit + okhttp

okhttp 3.9.0
retrofit 2.3.0

Other

kotlin 1.1.50
android support lib 26.1.0
google gms firebase 11.4.0
moshi 1.5.0
okio 1.13.0

Android Studio 3.0 Canary beta6
com.android.tools.build:gradle:3.0.0-beta6
com.google.gms:google-services:3.1.0
https://services.gradle.org/distributions/gradle-4.2-all.zip

compileSdkVersion 26
buildToolsVersion "26.0.2"

shrinkResources true
minifyEnabled true

Proguard Setting

# s - com.squareup.okhttp3
-dontwarn okio.**
-dontwarn javax.annotation.Nullable
-dontwarn javax.annotation.ParametersAreNonnullByDefault
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn com.squareup.okhttp.**
# e - com.squareup.okhttp3

# s - com.squareup.retrofit2
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn retrofit2.Platform$Java8
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
# e - com.squareup.retrofit2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment