Retrofit
和 Gson
有问题,我不知道为什么?
我的应用在发送登录数据时出错:
Unable to invoke no-args constructor for interface retrofit2.http.Url. Register an InstanceCreator with Gson for this type to fix this problem.
早些时候,我用 Java 制作了这个应用程序,代码完全相同,没有任何错误。但为什么现在使用 Kotlin?
主要事件:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText_login_user=findViewById<EditText>(R.id.Login_username)
val editText_login_password=findViewById<EditText>(R.id.Login_password)
val button_login=findViewById<Button>(R.id.btn_login)
button_login.setOnClickListener(View.OnClickListener { Connect_login(editText_login_user.text.toString(),editText_login_password.text.toString()) })
}
fun Connect_login(user:String,pass:String):Boolean{
var client= OkHttpClient()
var retrofit = Retrofit.Builder()
.baseUrl("http://sobosha.ir/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
val services=retrofit.create(ApiRet::class.java)
val call =services.Logindata(user,pass)
call.enqueue(object : Callback<User_info>{
override fun onResponse(call: Call<User_info>, response: retrofit2.Response<User_info>) {
val user: User_info? = response.body()
Toast.makeText(applicationContext, user?.name,Toast.LENGTH_LONG).show()
}
override fun onFailure(call: Call<User_info>, t: Throwable) {
Toast.makeText(applicationContext, t.message,Toast.LENGTH_LONG).show()
val editText_login_passwords=findViewById<EditText>(R.id.Login_password)
editText_login_passwords.setText(t.message)
}
})
return false
}
}
接口(interface):
public interface ApiRet {
@FormUrlEncoded
@POST("/Login.php")
Call<User_info> Logindata(@Field("username") String user, @Field("password") String pass);
}
和我的用户信息:
class User_info{
@SerializedName("name")
var name: String? = null
@SerializedName("image_url")
var image_url: Url? = null
@SerializedName("correct")
var correct:Boolean ?= null
}
我把界面改成这个
interface Apiservices{
@FormUrlEncoded
@POST("Login.php")
fun Login(@Field("username") user: String, @Field("password") pass: String): Call<User_info>
}
但它并没有改变错误。
最佳答案
首先,你不能使用retrofit2.http.Url
。它只是一个注解类。您可以将其更改为 HttpUrl
例如:
class User_info{
@SerializedName("name")
var name: String? = null
@SerializedName("image_url")
var image_url: HttpUrl? = null
@SerializedName("correct")
var correct:Boolean ?= null
}
然后您必须创建并注册自定义 url 反序列化器:
class UrlDeserializer : JsonDeserializer<HttpUrl> {
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): HttpUrl =
HttpUrl.get(json.asString)
}
val gson = GsonBuilder()
.registerTypeAdapter(HttpUrl::class.java, UrlDeserializer())
.create()
var retrofit = Retrofit.Builder()
.baseUrl("http://sobosha.ir/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
关于android-studio - 无法为接口(interface) retrofit2.http.Url 调用无参数构造函数。为这个类型注册一个Gson的InstanceCreator来解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839437/