0

I want to add data to Mysql via php party. But I keep failing. Check the data on the website. Still no solution. Where am I going wrong?

I tried the following and it doesn't seem to fix the problem. How to fix 'Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $' error in Android Studio and Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

I get another error, java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ from this error Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $. Adding GsonBuilder() .

I use //... to minimize the code.

dependencies

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:logging-interceptor:3.3.1"
implementation 'com.google.code.gson:gson:2.8.5'

layout

<EditText
    android:id="@+id/editText1"
    //... />

<EditText
    android:id="@+id/editText2"
    //... />

<EditText
    android:id="@+id/editText3"
    //... />

<Button
    android:id="@+id/buttonInsert"
    //...
     />

MainActivity

    class MainActivity : AppCompatActivity() {
        //...
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            //...
            binding.buttonInsert.setOnClickListener {
insert(editText1.text.toString(),editText2.text.toString(),editText3.text.toString())
            }
        }
    
        fun insert(name:String,alamal:String,telepon:String){
            val ardData:APIinsertData = InsertServer().konekRetrofit2()!!.create(APIinsertData::class.java)
            val tampilData: Call<ResponseModel?>? = ardData.insertData(name,alamal,telepon)
            tampilData?.enqueue(object :Callback<ResponseModel?>{
                override fun onResponse(
                    call: Call<ResponseModel?>,
                    response: Response<ResponseModel?>
                ) {
                    //...
                }
    
                override fun onFailure(call: Call<ResponseModel?>, t: Throwable) {
                    Log.d("ex","==${t.message.toString()}")
                }
            })
        }
    }

interface APIinsertData

interface APIinsertData {
    @FormUrlEncoded
    @POST("insert.php")
    open fun insertData(
        @Field("name")name:String,
        @Field("alamal")alamal:String,
        @Field("telepon")telepon:String
    ): Call<ResponseModel?>?
}

InsertServer

class InsertServer {
    private val baseURL="http://10.0.2.2/insertData/"
    private var retrofit: Retrofit?=null
    var gson = GsonBuilder()
        .setLenient()
        .create()

    fun konekRetrofit2(): Retrofit? {
        if (retrofit==null){
            retrofit= Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
        }
        return retrofit
    }
}

data class

data class DataModel (
    val id:Int,
    val name:String,
    val alamal:String,
    val telepon:String
        )
data class ResponseModel (
    val kode:Int,
    val pesan:String,
    val data:List<DataModel>
        )

The current simple information of MySql comes from response.body().

[DataModel(id=1, name=abc, alamal=cde, telepon=123),
 DataModel(id=4, name=adc, alamal=111, telepon=222)]
LoveCing
  • 45
  • 1
  • 7
  • The vote reflects usefulness of the question. It has no relation to your education. Please note that Stack Overflow is like Wikipedia. This is not a free help desk. Please don't take votes personally. The vote on your post has no impact on your learning ability. Please take the [tour] to learn more. – Dharman Feb 04 '22 at 09:12

1 Answers1

0

This response come from your server [DataModel(id=1, name=abc, alamal=cde, telepon=123), DataModel(id=4, name=adc, alamal=111, telepon=222)] so you set response serializable for api is wrong must be like this

interface APIinsertData {
@FormUrlEncoded
@POST("insert.php")
open fun insertData(
    @Field("name")name:String,
    @Field("alamal")alamal:String,
    @Field("telepon")telepon:String
): Call<List<DataModel>>?
}

change insertData response like above will work fine with you

yousef
  • 1,345
  • 1
  • 12
  • 20
  • Thank you. I tried your method. But I can't insert the data correctly. I only insert one DataModel data each time, it doesn't seem to be multiple data. My original mysql data is only two random creations. I don't want to add more Pen. – LoveCing Feb 04 '22 at 10:43
  • are you mean can send to server correctly? @LoveCing – yousef Feb 04 '22 at 12:12
  • I get onFailure() to give me Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $. – LoveCing Feb 04 '22 at 13:04
  • Are used my code change for interface APIinsertData ? so this failure come you expected come object but your server return array @LoveCing – yousef Feb 04 '22 at 13:45
  • I used your code change for interface APIinsertData. Then I fetched unsuccessfully it says Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $. – LoveCing Feb 04 '22 at 13:53
  • Please run your request on postman and show me response api result from postman ? – yousef Feb 04 '22 at 13:57
  • you need this.Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''id','name'.'alamal','telepon')VALUES(NULL,'','','')' at line 1 in C:\xampp\htdocs\insertData\insert.php:8 – LoveCing Feb 04 '22 at 15:15
  • Thank you friend, although you didn't hit it right, you let me know there is postman, although it took me a while to understand it. But the key place to help find the problem is the php problem. Thank you very much friend. – LoveCing Feb 05 '22 at 15:37