【发布时间】:2018-08-31 11:25:16
【问题描述】:
我正在尝试在 kotlin 中使用改造来访问 api
这是我的 DoinBackGround 方法
private fun doinBackground() {
Utility.printMessage("in do in background.....")
try {
val hdr = HashMap<String, String>()
hdr.put("x-csrf-token", Utility.getToken(this.context!!))
val apiInterface = ApiCallRetrofit.getClient(this.mCrypt!!)!!.create(ApiInterface::class.java)
if (what.equals(0)) {
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), getQuery(para))
print("header...")
call = apiInterface.hitApi(url, hdr, body)
} else if (what.equals(1)) {
val imgPart = ArrayList<MultipartBody.Part>()
if (files != null) {
if (files.size > 0) {
for (i in files.indices) {
imgPart.add(preparePart("image/*", "document_file[" + files.get(i).key + "]", files.get(i).file))
}
}
call = apiInterface.hitApiImage(url, hdr, getMap(para), imgPart)
}
call?.enqueue(object : Callback<StandardReposnse> {
override fun onResponse(call: Call<StandardReposnse>, response: Response<StandardReposnse>) {
try {
Utility.printMessage("messege...." + response.body().message)
val resp = Gson().toJson(response.body())
Utility.printMessage("Response :$resp")
Utility.longLogPrint(response.body().data, "Full response : ")
Utility.printMessage("Error : " + Gson().toJson(response.errorBody()))
onPostExecute(Parseresponce(response.body()))
} catch (e: Exception) {
Parseresponce(null)
e.printStackTrace()
}
}
override fun onFailure(call: Call<StandardReposnse>, t: Throwable) {
t.printStackTrace()
if (progressDialog != null) {
progressDialog?.dismiss()
}
Parseresponce(null)
}
})
}
} catch (e: Exception) {
e.printStackTrace()
}
}
这是我定义所有 POST 方法的界面
@POST
abstract fun hitApi(@Url api: String, @HeaderMap header: Map<String, Any>, @Body body: RequestBody): Call<StandardReposnse>
@POST
fun hitApiNoHeader(@Url api: String, @Body requestBody: RequestBody): Call<StandardReposnse>
@POST
fun test(@Url api: String, @HeaderMap headerMap: Map<String, String>, @Body requestBody: RequestBody): Call<JSONObject>
@Multipart
@POST
fun hitApiImage(@Url api: String, @HeaderMap headerMap: Map<String, String>, @PartMap bodyMap: Map<String, RequestBody>, @Part images: List<MultipartBody.Part>): Call<StandardReposnse>
每当我尝试使用 Api 时,我都会收到以下异常:
java.lang.IllegalArgumentException: Parameter type must not include a type variable or wildcard: java.util.Map<java.lang.String, ?> (parameter #2)
for method ApiInterface.hitApi
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:729)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:193)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
这是doinbackground方法发生异常的那一行
call = apiInterface.hitApi(url, hdr, body)
我在 RequestBody 之前尝试了@JvmSuppressWildcards 但它没有用,任何人都可以建议这里的实际问题是什么,而且虽然我使用了 print() 函数,但日志中没有打印任何内容,我应该使用 LOG.d 吗?
【问题讨论】: