请求在Android中进行改造
我想在我的应用中使用请求帖子。 我使用了这个,但我有问题。 当我想创建一个帐户时,我会面临这个问题。
状态代码403
您可以看到我的代码:
api.kt
interface Api {
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("users/signup")
@FormUrlEncoded
fun createUser(
@Field("first_name") fName: String,
@Field("last_name") lName: String,
@Field("email") email: String,
@Field("username") username: String,
@Field("password") password: String,
) : Call<UserResponse>
}
userresponse.kt
data class UserResponse(
val email: String,
val first_name: String,
val password: String,
val last_name: String,
val username: String
)
Inbupactivity.kt
binding.btnSignUp
.setOnClickListener {
val username = binding.etUsername.text.toString().trim()
val password = binding.etPw.text.toString().trim()
val fName = binding.etFName.text.toString().trim()
val lName = binding.etLName.text.toString().trim()
val email = binding.etEmail.text.toString().trim()
if (username.isEmpty()) {
binding.etUsername.error = "Password required"
binding.etUsername.requestFocus()
return@setOnClickListener
}
if (password.isEmpty()) {
binding.etPw.error = "Password required"
binding.etPw.requestFocus()
return@setOnClickListener
}
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor(logging)
val instance: Api by lazy {
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
retrofit.create(Api::class.java)
}
instance.createUser(fName, lName, email, username, password)
.enqueue(object : Callback<UserResponse> {
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<UserResponse>,
response: Response<UserResponse>
) {
Toast.makeText(applicationContext, response.code(), Toast.LENGTH_LONG)
.show()
}
})
}
我在Postman上使用此请求,并且效果很好
但是在Android Studio中它不起作用 我有这个错误 在这里输入图像描述
我希望你能帮助我
I want use request post in my app.
I used this but I have a problem.
I face this problem when I want to create an account.
status code 403
You can see my code:
Api.kt
interface Api {
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("users/signup")
@FormUrlEncoded
fun createUser(
@Field("first_name") fName: String,
@Field("last_name") lName: String,
@Field("email") email: String,
@Field("username") username: String,
@Field("password") password: String,
) : Call<UserResponse>
}
UserResponse.kt
data class UserResponse(
val email: String,
val first_name: String,
val password: String,
val last_name: String,
val username: String
)
SignUpActivity.kt
binding.btnSignUp
.setOnClickListener {
val username = binding.etUsername.text.toString().trim()
val password = binding.etPw.text.toString().trim()
val fName = binding.etFName.text.toString().trim()
val lName = binding.etLName.text.toString().trim()
val email = binding.etEmail.text.toString().trim()
if (username.isEmpty()) {
binding.etUsername.error = "Password required"
binding.etUsername.requestFocus()
return@setOnClickListener
}
if (password.isEmpty()) {
binding.etPw.error = "Password required"
binding.etPw.requestFocus()
return@setOnClickListener
}
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor(logging)
val instance: Api by lazy {
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
retrofit.create(Api::class.java)
}
instance.createUser(fName, lName, email, username, password)
.enqueue(object : Callback<UserResponse> {
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<UserResponse>,
response: Response<UserResponse>
) {
Toast.makeText(applicationContext, response.code(), Toast.LENGTH_LONG)
.show()
}
})
}
I use this request on postman and it works good
but in android studio it didn't work
and I have this error
enter image description here
I hope u can help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一些修改,您可以进行:
步骤:1在
androidMenifest.xml
文件步骤中添加权限
此
。将此方法添加到您的活动中,并在您的ongreate()或适当的位置中调用方法
apicall()
。步骤:4响应在屏幕截图中
快乐编码:-)
Here are the some modification, you can make :
step: 1 Add this permission in your
AndroidMenifest.xml
filestep: 2
UserResponse.java
Step: 3 -> Add this methods to your activity and call method
apiCall()
in your onCreate() or appropriate place.Step: 4 Response is in the screen shot
Happy Coding :-)