改造 POST jsonObject 不上传

我已经为此工作了几个小时,但没有取得任何进展。我是改造新手,基本上只是尝试使用 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()

        }




饮歌长啸
浏览 129回答 2
2回答

慕工程0101907

亲爱的朋友。我建议将以下两行添加到 build.gradle 文件中。&nbsp; &nbsp; // Logging Interceptor&nbsp; &nbsp; implementation "com.squareup.okhttp3:logging-interceptor:4.7.2"&nbsp; &nbsp; implementation 'org.conscrypt:conscrypt-android:2.4.0'并如下所示使用它object ApiServiceContainer {&nbsp; &nbsp; private var apiService: ApiService? = null&nbsp; &nbsp; fun getApiService(): ApiService {&nbsp; &nbsp; &nbsp; &nbsp; if (apiService == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val logging = HttpLoggingInterceptor()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logging.level = HttpLoggingInterceptor.Level.BODY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val httpClient = OkHttpClient.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpClient.addInterceptor { chain ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val original = chain.request()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val requestBuilder = original.newBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .header("Authorization", "Bearer " + token)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .header("Accept", "application/json")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .header("Content-Type", "application/json")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val request = requestBuilder.build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chain.proceed(request)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpClient.connectTimeout(30, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpClient.readTimeout(30, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpClient.addNetworkInterceptor(logging)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val okHttpClient = httpClient.build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val gson = GsonBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setLenient()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .create()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val retrofit = Retrofit.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .baseUrl(EndPoints.API_BASE_URL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConverterFactory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GsonConverterFactory.create(gson)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addCallAdapterFactory(RxJava2CallAdapterFactory.create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .client(okHttpClient)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiService = retrofit.create(ApiService::class.java)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return apiService!!&nbsp; &nbsp; }这样你就可以看到服务器发出的错误。

噜噜哒

我发现,无论出于何种原因,在 php 中接收时,发送除数组或字段之外的任何内容都会添加额外的字符,我最终只是使用这样的字段。interface ApiInterface {@FormUrlEncoded@POST(BuildConfig.URL_REGISTER)fun signUpUser(@Field("username") username: String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Field("password") password: String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Field("email") email: String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Field("termsOfService") termsOfService: Boolean,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Field("profilePicPath") profilePicPath: String): Call<Response>}在 php 中它是这样接收的$username = $_POST['username'];$password = $_POST['password'];$email = $_POST['email'];$termsOfService = $_POST['termsOfService'];$profilePicPath = $_POST['profilePicPath'];
打开App,查看更多内容
随时随地看视频慕课网APP