2020年12月25日星期五

How should I create a PHP script for Retrofit Post method

I have a MySQL database in JSON format as below

{  "list": [      {          "email": "peter@mail.com",          "date": "21.21.2019"      }  ]  }  

and I want to insert my AppModel as an object,

data class AppModel(  val email: String,  val date: String  )  

my insert.php script is as below

<?php  $mysqli = new mysqli("localhost", "xxx", "xxx", "xxx");    // Check connection  if($mysqli === false){      die("ERROR: Could not connect. " . $mysqli->connect_error);  }    // Attempt insert query execution  $sql = "INSERT INTO db (email, date) VALUES ('peter@mail.com','21.21.2019')";  if($mysqli->query($sql) === true){      echo "Records inserted successfully.";  } else{      echo "ERROR: Could not able to execute $sql. " . $mysqli->error;  }    // Close connection  $mysqli->close();  ?>  

the problem is I send a JSON object but my database has an JSON array and I am getting this error

 Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1   path $  

I think have to change some codes in my php script but I don't how I should do it.

Below is my Kotlin code

object RetrofitInstance {        private val interceptor = HttpLoggingInterceptor()        init {          interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)      }        private val client = OkHttpClient.Builder()          .addInterceptor(interceptor)          .addNetworkInterceptor(Interceptor { chain ->              val request: Request = chain.request().newBuilder()                  .addHeader("projectId", "xxx")                  .build()              chain.proceed(request)          })          .build()        private var gson: Gson = GsonBuilder()          .setLenient()          .create()        private val retrofit by lazy {          Retrofit.Builder()              .baseUrl("http://a...")              .addConverterFactory(GsonConverterFactory.create(gson))              .client(client)              .build()      }        val api: SimpleApi by lazy {          retrofit.create(SimpleApi::class.java)      }    }          interface SimpleApi {        @POST("insert.php")      suspend fun pushAppModel(          @Body appModel: AppModel      ): Response<AppModel>    }          class Repository {        suspend fun pushAppModel(appModel: AppModel): Response<AppModel> {          return RetrofitInstance.api.pushAppModel(appModel)      }    }        class HomeViewModel(      private val repository: Repository  ) : ViewModel() {        private var myResponse: MutableLiveData<Response<AppModel>> = MutableLiveData()        fun pushAppModel(appModel: AppModel) {          viewModelScope.launch {              val response = repository.pushAppModel(appModel)              myResponse.value = response          }      }    }      class HomeViewModelFactory(private val repository: Repository): ViewModelProvider.Factory {      override fun <T : ViewModel?> create(modelClass: Class<T>): T {          return HomeViewModel(repository) as T      }  }    // in related fragment  val repository = Repository()  val viewModelFactory = HomeViewModelFactory(repository)  viewModel = ViewModelProvider(this, viewModelFactory).get(HomeViewModel::class.java)  viewModel.pushAppModel(AppModel("abc@abc.com", "11.11.2020"))  
https://stackoverflow.com/questions/65453056/how-should-i-create-a-php-script-for-retrofit-post-method December 26, 2020 at 09:37AM

没有评论:

发表评论