我已经为此工作了几个小时,但没有取得任何进展。我是改造新手,基本上只是尝试使用 jsonObject 发出简单的发布请求,但收到此错误:
java.lang.illegalStateException: Expected BEGIN_OBJECT but was STRING at line
1 column 1 path $
我尝试过将客户端代码的几乎每一部分更改为使用 multipart 或 requestBody 甚至我的 php 代码,但没有任何效果,还尝试回显脚本中的输入,但它打印为空。非常感谢您的每一个回复,谢谢。
界面
interface ApiInterface {
@Headers("Content-Type: application/json")
@POST(BuildConfig.URL_REGISTER)
fun signUpUser(@Body jsonObject: JSONObject): Call<Response>
}
回复
data class Response(@SerializedName("success") val success: Boolean,
@SerializedName("message") val message: String,
@SerializedName("userId") val userId:String)
客户
class ApiClient {
private external fun register(): String
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
private val okhttpClient = OkHttpClient.Builder().build()
private val gson: Gson = GsonBuilder()
.setLenient()
.create()
val instance: ApiInterface by lazy {
val retrofit = Retrofit.Builder()
.baseUrl(register())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okhttpClient)
.build()
retrofit.create(ApiInterface::class.java)
}
要求
val jsonObject = JSONObject()
jsonObject.put("username", username)
jsonObject.put("password", password)
jsonObject.put("email", email)
jsonObject.put("termsOfService", termsOfService)
jsonObject.put("profilePicPath", imagePath)
ApiClient().instance.signUpUser(jsonObject).enqueue(object: Callback<com.passionit.picnbuy.models.Response>{
override fun onFailure(
call: Call<com.passionit.picnbuy.models.Response>, t: Throwable) {
Toast.makeText(this@LoginOrSignUpActivity, t.message, Toast.LENGTH_LONG).show()
}
慕工程0101907
噜噜哒