使用Retrofit + OkHttp + Gson api返回html虽然postman返回json

问题描述 投票:1回答:1

我在邮递员中测试了我的api,它返回了json

只是一个普通的api

的BaseURL / COM-服务/火/移动/任务/用户/ tokenhere_ /即将到来

但在Android上

  @GET("mobile/tasks/user/{id}/upcoming")
  fun jobList(@Path("id") id: String): retrofit2.Call<Job>

这会返回一个html

onError返回:

java.lang.IllegalStateException:预期为BEGIN_OBJECT但在第1行第1行为STRING路径$

这是电话

 fun fetchData() {
    apiService.jobList(getUserId()).enqueue(object : retrofit2.Callback<Job>{
      override fun onFailure(call: Call<Job>?, t: Throwable?) {
        Timber.v(t!!.message.toString())
      }
      override fun onResponse(call: Call<Job>?, response: Response<Job>?) {
          Timber.v(response!!.body().toString())
      }
    })

工作=数据

JSON:

{
  "type": "success",
  "text": "200",
  "data": [
    {
      "requestId": "5a31fc2b9af83128136f8fb3",
      "taskDetailRequestId": null,
      "clientEmail": null,
      "imageBase64": null,
      "imageLogoWidth": 0,
      "imageLogoHeight": 0,
      "additionalEmails": [],
      "clientId": "5891d6b01d58301538b578ea",
      "clientServiceAddress": {
        "id": "5891d6b01d58301538b578eb",
        "_refs": null,
        "name": "tony chew",
        "address1": null,
        "address2": null,
        "pinCode": "123123",
        "createdUserId": null,
        "lastUpdatedUserId": null,
        "latitude": 0,
        "longitude": 0,
        "organizationId": null,
        "addressType": null
      },
      "startDate": 1513311600000,
      "endDate": 1513312500000,
      "userRegId": "5891d7521d58301538b578fd",
      "requestDate": 1513225200000,
      "serviceName": "Service 1 SImple",
      "serviceRequest": null,
      "userTaskDetailsId": "5a31fc319af83128136f8fb9",
      "fromSubclient": false,
      "requestStatus": "ASSIGNED",
      "serviceRequestType": "SERVICE",
      "contractPayments": null
    }
  ]
}

Job.kt

data class Job(
  @Expose @SerializedName("type") val type: String, //success
  @Expose @SerializedName("text") val text: String, //200
  @Expose @SerializedName("data") val  data: List<Data>
)
android kotlin retrofit2 swagger-ui okhttp3
1个回答
0
投票

可能您需要提供Accept标头

http://square.github.io/retrofit/#api-declaration

@Headers("Accept: application/json")
@GET("mobile/tasks/user/{id}/upcoming")
  fun jobList(@Path("id") id: String): retrofit2.Call<Job>

但是,由于其他原因(例如auth和重定向到某处),您的请求也可能失败。首先尝试接受标头。但是你没有在你的问题中提供足够的细节来明确回答。

© www.soinside.com 2019 - 2024. All rights reserved.