0

I am trying to send a string type list to mysql database with retrofit. When I try with postman I get success but I am getting error message from android studio.

My API Interface :

@FormUrlEncoded
@POST("yesilegeservices/insertJob.php")
fun insertJob  (@Field("Token")         token:String, 
                @Field("StartingDate")  StartingDate:String, 
                @Field("TargetTime")    TargetTime:String,
                @Field("JobContent")    JobContent:String,
                @Field("StaffList[]")   StaffList:List<String>,  // My List
                @Field("ReleaseDate")   ReleaseDate:String, 
                @Field("KindId")        kindId:String):Single<JobInsertResponse>

My data Class for Response :

data class JobInsertResponse(
@SerializedName("job") @Expose val incomingId:List<singleUuid>,
@SerializedName("jobInsertSuccess") val success:Int
)

data class singleUuid (
@SerializedName("JobId") @Expose val uuid:Int
)

Php response :

{"job":[{"JobId":"15"}],"jobInsertSuccess":1}

Android studio error message:

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

OkHttp log :

2021-07-02 15:49:31.350 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: --> POST 

http://10.0.2.2:8888/yesilegeservices/insertJob.php
2021-07-02 15:49:31.350 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Content-Type: application/x-www-form- 
urlencoded

2021-07-02 15:49:31.351 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Content-Length: 211

2021-07-02 15:49:31.352 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: 
Token=yN4XGdFIzIMOrgE3Q4rB1jqoNJUbpfOqHI3IuuiUU&StartingDate=02- 
07- 
2021&TargetTime=15.49&JobContent=lorem%20impsun%20dolor&
StaffList%5B%5D=10&StaffList%5B%5D=11&StaffList%5B%5D=12
&ReleaseDate=02-07-2021&KindId=1

2021-07-02 15:49:31.352 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: --> END POST (211-byte body)
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: <-- 200 OK 

http://10.0.2.2:8888/yesilegeservices/insertJob.php (28ms)
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Date: Fri, 02 Jul 2021 12:49:39 GMT

2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Server: Apache/2.4.46 (Unix) 
OpenSSL/1.0.2u PHP/8.0.0 mod_wsgi/3.5 Python/2.7.13 
mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 

Perl/v5.30.1
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: X-Powered-By: PHP/8.0.0
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Content-Length: 98
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Keep-Alive: timeout=5, max=100
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Connection: Keep-Alive

2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Content-Type: text/html; charset=UTF-8
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient:                 
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient:             
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: Incorrect date value: '02-07-2021' for 
column 'ReleaseDate' at row 1
2021-07-02 15:49:31.381 16777-16916/com.thic.yesilege 
I/okhttp.OkHttpClient: <-- END HTTP (98-byte body)   

I think the problem is in the list I posted because If I delete the list, the result is successful.

What's wrong ?

  • If it is possible, share the android app's request and response in JSON format. Input and output from Android. You can add okhttp log interceptor to get it. – Hassan Alizadeh Jul 02 '21 at 11:50
  • Have you seen this https://stackoverflow.com/questions/28418662/expected-begin-object-but-was-string-at-line-1-column-1 – Mustafo aka Shokhrukh Makmudov Jul 02 '21 at 11:52
  • @HassanAlizadeh I never used OkHttp. I have no idea how to do this – Cemal Tüysüz Jul 02 '21 at 12:00
  • @CemalTüysüz inside of retrofit, you always have okhttp. Please check this [link](https://github.com/HassanAlizadehDevlab/Movie-Star/blob/master/app/src/main/java/com/android/shared/data/network/NetworkManager.kt). this how you can use HTTP logging interceptor. Before using this, you should implement this in your app/build.gradle file -> `implementation "com.squareup.okhttp3:logging-interceptor:4.8.1`. Then you can see OkHttp logs in you logcat. – Hassan Alizadeh Jul 02 '21 at 12:14
  • @HassanAlizadeh I added the log to the question – Cemal Tüysüz Jul 02 '21 at 12:33
  • @CemalTüysüz there is a mistake at this line`logging.level = HttpLoggingInterceptor.Level.BASIC`. Change it to this please `logging.level = HttpLoggingInterceptor.Level.BODY`. After that replace the log again, please. – Hassan Alizadeh Jul 02 '21 at 12:37
  • @HassanAlizadeh I think i found it , mysql database date format accepted: "yyyy-MM-dd" but my date format = "dd-MM-yyyy" . Maybe this is another problem. I updated the logs in this question. – Cemal Tüysüz Jul 02 '21 at 12:56
  • @HassanAlizadeh Yes , problem is date. Question is resolved. Thank you so much... – Cemal Tüysüz Jul 02 '21 at 13:16
  • @CemalTüysüz YW :) – Hassan Alizadeh Jul 02 '21 at 13:20

2 Answers2

0

I think problem is in your data class.Your jobs arraylist containts objects and inside objects there is a string of jobId but you are parsing it as integer.

Try this solution it might help.

data class JobInsertResponse(
        @SerializedName("job") @Expose val incomingId:List<singleUuid>,
        @SerializedName("jobInsertSuccess") val success:Int
    )

    data class singleUuid (
        @SerializedName("JobId") @Expose val uuid:String
    )
Muhammad Zahab
  • 1,049
  • 10
  • 21
0

Date is problem. Mysql database accepted date format = "yyyy-MM-dd" but my date format "dd-MM-yyyy".