2 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
親愛的朋友。我建議將以下兩行添加到 build.gradle 文件中。
// Logging Interceptor
implementation "com.squareup.okhttp3:logging-interceptor:4.7.2"
implementation 'org.conscrypt:conscrypt-android:2.4.0'
并如下所示使用它
object ApiServiceContainer {
private var apiService: ApiService? = null
fun getApiService(): ApiService {
if (apiService == null) {
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.header("Authorization", "Bearer " + token)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
val request = requestBuilder.build()
chain.proceed(request)
}
httpClient.connectTimeout(30, TimeUnit.SECONDS)
httpClient.readTimeout(30, TimeUnit.SECONDS)
httpClient.addNetworkInterceptor(logging)
val okHttpClient = httpClient.build()
val gson = GsonBuilder()
.setLenient()
.create()
val retrofit = Retrofit.Builder()
.baseUrl(EndPoints.API_BASE_URL)
.addConverterFactory(
GsonConverterFactory.create(gson)
)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
apiService = retrofit.create(ApiService::class.java)
}
return apiService!!
}
這樣你就可以看到服務(wù)器發(fā)出的錯(cuò)誤。

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
我發(fā)現(xiàn),無論出于何種原因,在 php 中接收時(shí),發(fā)送除數(shù)組或字段之外的任何內(nèi)容都會(huì)添加額外的字符,我最終只是使用這樣的字段。
interface ApiInterface {
@FormUrlEncoded
@POST(BuildConfig.URL_REGISTER)
fun signUpUser(@Field("username") username: String,
@Field("password") password: String,
@Field("email") email: String,
@Field("termsOfService") termsOfService: Boolean,
@Field("profilePicPath") profilePicPath: String): Call<Response>
}
在 php 中它是這樣接收的
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$termsOfService = $_POST['termsOfService'];
$profilePicPath = $_POST['profilePicPath'];
- 2 回答
- 0 關(guān)注
- 165 瀏覽
添加回答
舉報(bào)