Tag: 改造2

改进API调用:如何确保在进行api调用后该值不为空?

我有下面的AuthenticationResponse模型,我使用retrofit2进行3个API调用。 如果我进行了verifyEmail调用,那么JSON响应主体只包含有效的电子邮件属性(如{“validEmail”:true})。 其他2个调用只包含“resetSuccesful”或其他4的属性。 我怎样才能确保/检查,当我收到verifyEmail调用的响应fe,它包含一个非空的validEmail值? 服务: interface AuthenticationService { @POST(“auth/checkEmail”) fun verifyEmail(@Body email: String): Call @POST(“auth/login”) fun login(@Body loginCredentials: LoginCredentials): Call @POST(“auth/resetPassword”) fun resetPassword(@Body email: String): Call } 模型: data class AuthenticationResponse( val validEmail: Boolean? = null, val loginSuccess: Boolean? = null, val retriesLeft: Int? = null, val authToken: String? = null, val accountBlocked: Boolean? = […]

call.enqueue正在阻塞第一个请求

我在Kotlin编写的Android应用程序中使用Retrofit 2来调用API。 我的电话如下所示: val time = measureTimeMillis { val call = geocodingApi.searchByName(“Berlin”) call.enqueue(object : Callback { override fun onResponse(call: Call?, response: Response?) { // some code } override fun onFailure(call: Call?, t: Throwable?) { // some code } }) } Timber.d(“searching by name took $time ms”) geocodingApi在我的类的构造函数中初始化如下: val loggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { Timber.d(it) }) loggingInterceptor.level […]

KotlinReflectionInternalError数据类

尝试在Retrofit服务中使用数据类作为结果types时收到以下错误。 11-15 11:35:38.345 14693-14693/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.app, PID: 14693 kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Incorrect resolution sequence for Java constructor public constructor AuthenticationResponse() defined in com.example.app.data.auth.AuthenticationResponse[JavaClassConstructorDescriptor@6d57dc3] (kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass: class com.example.app.data.auth.AuthenticationResponse) at kotlin.reflect.jvm.internal.RuntimeTypeMapper.mapSignature(RuntimeTypeMapper.kt:202) at kotlin.reflect.jvm.internal.KFunctionImpl.(KFunctionImpl.kt:46) at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:87) at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:39) at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93) at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32) at kotlin.reflect.jvm.internal.KClassImpl$Data.getConstructors(Unknown Source:7) at kotlin.reflect.jvm.internal.KClassImpl.getConstructors(KClassImpl.kt:192) at kotlin.reflect.full.KClasses.getPrimaryConstructor(KClasses.kt:40) at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:160) at com.squareup.moshi.Moshi.adapter(Moshi.java:100) at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:90) at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328) […]

Android 6上的改造2设置了零路径参数

我正在使用Retrofit 2库版本2.3.0,并突然开始用零代替整数路径参数,而不是实际的数字。 我正在使用Kotlin(1.2.21),我有一个方法,如: @GET(“posts/{postId}”) fun getPosts(@Path(“postId”) postId: Long): Single 这工作应该,但现在当我在Android 6上运行我的应用程序(设备和模拟器) postId路径参数始终是零即调用posts/0 。 Android 7和8都很好。 Retrofit和OkHttp版本没有改变,界面也没有改变。 我已经查看了构建的请求,并且OkHttpCall里面有一个带有值为0的Longtypes的值的数组。这只是在Android 6上,其他人在那里有正确的值。 ServiceMethod在接口中设置了正确的URL,它有一个ParameterHandler用于BuiltInConverter.ToStringConvertertypes的postId 。 应用程序是multiDexed和ProGuard不使用。 会发生什么?

用翻新和moshi填充微调

我是Android开发人员(包括java或kotlin)的初学者。 我正在尝试用改造和moshi从json填充微调器,但我不知道如何将其填充到微调器中。 说实话,我不知道Json数据的返回是否正确,因为Log.d()返回不是作为dump()laravel或php的细节。 脚本在活动onCreate(请阅读脚本的评论,我把Log.d()调试结果) Log.d() val task = object : AsyncTask<Void, Void, Response<List>>() { override fun doInBackground(vararg params: Void): Response<List> { val typeAPI = RestAPI() val callResponse = typeAPI.getNews() val response = callResponse.execute() return response } override fun onPostExecute(response: Response<List>) { if (response.isSuccessful) { val news = response.body() Log.d(“test:”, news!![0].data.toString()) // method ‘java.lang.String com.example.mockie.tigaer.api.TypeDataResponse.toString()’ on […]

如何在Retrofit 2中处理POST请求响应?

我试图将appVersion和deviceName作为String发送到服务器,并获取systemDate和token为字符串作为响应。 这是我在Kotlin的POJO: class InitPost { @SerializedName(“appVersion”) @Expose var versionName: String? = null @SerializedName(“deviceName”) @Expose var manufacturer: String? = null } Api服务: @POST(“getRequest”) Call postInit(@Body InitPost initPost); 在我的活动中: apiService.postInit(initPost) .enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if(response.isSuccessful()){ Timber.d(“INIT POST SUCCESSFUL”); } } @Override public void onFailure(Call call, Throwable t) { Timber.e(“INIT POST […]

快速翻新吗?

我现在正在使用改造。 另外,我使用下面的库。 ================= GSON-2.8.2.jar GSON-2.8.2-javadoc.jar hamcrest核-1.3.jar 基于JUnit 4.12.jar okhttp-3.9.1.jar 奥基奥-1.13.0.jar 改装的2.3.0.jar ================ 问:复古适合速度真快吗? 由于我的测试,它太慢了。 平均速度:2500毫秒,个人代码平均速度:900毫秒 这是真的,我正确地使用它? (科特林) 以下是使用Retrofit的代码。 interface ApiService { @GET(“/lol/summoner/v3/summoners/by-name/{name}”) fun getSummonerByName(@Path(“name”) name : String, @Query(“api_key”) apiKey : String): Call } fun getSummonerByName(summonerName: String, apiKey: String): SummonerDTO? { var retrofit = Retrofit.Builder().baseUrl(“https://” + HOST + “”).addConverterFactory(GsonConverterFactory.create()).build() var service = retrofit.create(ApiService::class.java) var repos = […]

改造 – 分析意外的数据types

我正在Android Kotlin项目中使用Retrofit库 并想处理解析意外的数据types。 例如,如果JSON: { “boolean”: true, “number”: “123”, “string”: “Hello World” } 数据类将是 data class Response ( val boolean: Boolean? = null, val number: Int? = null, val string: String? = null ) 通常情况下,Retrofit将无法解析响应,并且onFailure()将被调用,因为改装不能解析属性number因为它是以字符串forms返回的,并且期望编号,整个对象将无法解析。 我想要的,正常处理请求只是未分析的字段应该是空的。

编写自定义Retrofit适配器的通用问题

在过去,我已经编写了基于此代码的自定义error handling适配器进行改进: https : //github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/ErrorHandlingAdapter.java 哪些工作真的很好。 我现在正在Kotlin编写我们的新应用程序,当转换代码时,呼叫适配器工厂给我一个错误,特别是在get函数中。 它转换成这样的样子: override fun get(returnType: Type, annotations: Array, retrofit: Retrofit): CallAdapter? { if (CallAdapter.Factory.getRawType(returnType) != MyCall::class) { return null } if (returnType !is ParameterizedType) { throw IllegalStateException( “MyCall must have generic type (eg, MyCall)”) } val responseType = CallAdapter.Factory.getParameterUpperBound(0, returnType) val callbackExecutor = retrofit.callbackExecutor() return ErrorHandlingCallAdapter(responseType, callbackExecutor). \\This line […]

如何在Kotlin中使用Retrofit和Observable创建异步调用?

我想使用Retrofit2库进行API调用,返回genericstypesobservable。 我得到一个错误:android.os.NetworkOnMainThreadException,同时进行调用。