我找到了回应: 我只是忘记了在loginAction()方法中使用Gson将响应解析为json
private fun loginAction(user: String)
{
UserService.userLogin(user, {
response ->
val token = response.header("x-auth")
val userData = Gson().fromJson(response.body()!!.string(), User::class.java)
createUserSession(baseContext, userData, token)
this.openHomeActivity()
},
{
error ->
Log.d("ERROR", error.toString())
})
}
我正在尝试使用外部API检查电子邮件和密码来向我的移动应用程序添加登录活动。 我使用OkHttp 3.14.0连接到API,但是在尝试连接用户时出现该错误:
2019-04-02 13:37:10.630 5118-5176/fr.myapp.firstapp E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: fr.myapp.firstapp, PID: 5118
java.lang.Error: org.json.JSONException: End of input at character 0 of
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:176)
at fr.myapp.firstapp.LoginActivity$loginAction$1.invoke(LoginActivity.kt:44)
at fr.myapp.firstapp.LoginActivity$loginAction$1.invoke(LoginActivity.kt:15)
at fr.myapp.firstapp.network.UserService$Companion$userLogin$1.onResponse(UserService.kt:21)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
这是我的登录活动:
class LoginActivity : AppCompatActivity() {
lateinit var usernameInput: EditText
lateinit var passwordInput: EditText
lateinit var loginBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_activity)
this.usernameInput = findViewById(R.id.login_username_input)
this.passwordInput = findViewById(R.id.login_password_input)
this.loginBtn = findViewById(R.id.login_action_btn)
loginBtn.setOnClickListener { this.loginAction(this.loginInfo()) }
}
private fun openHomeActivity()
{
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}
private fun loginAction(user: String)
{
UserService.userLogin(user, {
response ->
val jsonData: String = response.body()!!.string()
val userObj = JSONObject(jsonData)
createUserSession(this, userObj)
this.openHomeActivity()
},
{
error ->
Log.d("ERROR", error.toString())
})
}
private fun loginInfo(): String
{
val username: String = this.usernameInput.text.toString()
val password: String = this.passwordInput.text.toString().sha512()
val data = JSONObject()
.put("username", username)
.put("password", password)
return data.toString()
}
}
和我的用户服务:
class UserService {
companion object {
private val JSON = MediaType.get("application/json; charset=utf-8")
fun userLogin(data: String, success: (success: Response) -> Unit, failure: (error: IOException) -> Unit)
{
val body = RequestBody.create(this.JSON, data)
val request: Request = Request.Builder()
.url(UrlBuilder.api + "/user/login")
.post(body)
.build()
OkHttpClient().newCall(request).enqueue(object: Callback {
override fun onResponse(call: Call, response: Response) = success(response)
override fun onFailure(call: Call, e: IOException) = failure(e)
})
}
}
我还在我的AndroidManifest.xml中添加了连接到Internet的授权:
<uses-permission android:name="android.permission.INTERNET"/>
谢谢您的帮助
PS:对不起,我的英语,这不是我的主要语言
答案 0 :(得分:0)
我找到了响应:我只是忘了在loginAction()方法中使用Gson将响应解析为json
private fun loginAction(user: String)
{
UserService.userLogin(user, {
response ->
val token = response.header("x-auth")
val userData = Gson().fromJson(response.body()!!.string(), User::class.java)
createUserSession(baseContext, userData, token)
this.openHomeActivity()
},
{
error ->
Log.d("ERROR", error.toString())
})
}