У меня есть некоторые проблемы с retrofit2 в моем первом тестовом проекте Android Kotlin.и мое приложение показывает сообщение об ошибке - Конец ввода в строке 1, столбец 1, путь $
Кто-нибудь сталкивался с этой проблемой?
Я добавил это разрешение
<uses-permission android:name="android.permission.INTERNET"/>
Build Gradle
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
Журнал HTTP
D/OkHttp: --> POST http://192.168.1.106/login.php
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 29
D/OkHttp: login=admin&password=ADr00tAD
D/OkHttp: --> END POST (29-byte body)
D/OkHttp: <-- 200 OK http://192.168.1.106/login.php (48ms
D/OkHttp: Server: nginx/1.14.2
D/OkHttp: Date: Thu, 02 May 2019 10:13:39 GMT
D/OkHttp: Content-Type: text/html; charset=UTF-8
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Connection: keep-alive
D/OkHttp: <-- END HTTP (0-byte body)
D/Error:::: End of input at line 1 column 1 path $
проверить мой сервер API отдыха https://i.ibb.co/cTFsQ8J/Screenshot-from-2019-05-02-16-14-33.png
<- Интерфейс IMyAPI ->
package com.example.root.myapplication.Remote
import com.example.root.myapplication.Model.APIResponse
import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST
interface IMyAPI {
@FormUrlEncoded
@POST("login.php")
fun loginUser(@Field("login") login: String, @Field("password") password: String):Call<APIResponse>
}
<- Класс APIResponse ->
package com.example.root.myapplication.Model
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class APIResponse {
@SerializedName("error")
@Expose
var error: Boolean=false
@SerializedName("err_msg")
@Expose
var err_msg:String?=null
var User:User?=null
}
<- Пользователь класса ->
package com.example.root.myapplication.Model
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class User {
@SerializedName("name")
@Expose
var name: String = ""
@SerializedName("email")
@Expose
var email: String = ""
@SerializedName("role")
@Expose
var role: String = ""
}
<- Объект RetrofitClass ->
package com.example.root.myapplication.Remote
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
object RetrofitClass{
private var retrofit:Retrofit?=null
fun getClient(baseUrl:String):Retrofit{
if (retrofit == null)
{
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()
retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
return retrofit!!
}
}
<- Object Common ->
package com.example.root.myapplication.Common
import com.example.root.myapplication.Remote.IMyAPI
import com.example.root.myapplication.Remote.RetrofitClass
object Common {
val BASE_URL = "http://192.168.1.106/" //I'm using my phone
val api: IMyAPI
get() = RetrofitClass.getClient(BASE_URL)!!.create(IMyAPI::class.java)
}
<- MainActivity ->
package com.example.root.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.example.root.myapplication.Common.Common
import com.example.root.myapplication.Model.APIResponse
import com.example.root.myapplication.Remote.IMyAPI
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MainActivity : AppCompatActivity() {
internal var mServise:IMyAPI? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mServise = Common.api
println(mServise)
btn_login.setOnClickListener{authenticateUser(edt_login.text.toString(),edt_password.text.toString())}
}
private fun authenticateUser(login: String, password: String) {
mServise!!.loginUser(login,password)
.enqueue(object :Callback<APIResponse>{
override fun onFailure(call: Call<APIResponse>?, t: Throwable?) {
//Toast.makeText(this@MainActivity, response!!.body,Toast.LENGTH_LONG).show()
Toast.makeText(this@MainActivity, t!!.message,Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call<APIResponse>?, response: Response<APIResponse>?) {
if (response!!.body()!!.error)
Toast.makeText(this@MainActivity,response!!.body()!!.err_msg,Toast.LENGTH_SHORT).show()
else
Toast.makeText(this@MainActivity,"Login Success!!!",Toast.LENGTH_SHORT).show()
}
})
}
}