android volley在workmanager中的返回响应

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

我一直在尝试使用Workmanager实现android volley来上传背景数据。实现完全没有问题,但是通过Result.success注册并返回响应结果确实很困难。

override fun doWork(): Result {


    var volley = VolleySingleTon.getInstance(mContext)
        .syncLocationData(mContext, location, success = Function { collection ->

        ERROR HERE************
           **Need to return the return Result.success()**
           **but i can't return the above**

        }, failure = Function { collection ->

        ERROR HERE************
           **Need to return the return Result.failure()**
          **but i can't return the above**
        })


}

排球请求方法类

fun syncLocationData(
    context: Context,
    location: List<VehicleLocation>, success: Function<RestCollection, Any>, failure: Function<RestCollection, Any>
) {

    var jsonObject: JSONObject? = null
    val jsonString = RestUtil.getGson().toJson(location)

    try {
        jsonObject = JSONObject(jsonString)
    } catch (e: JSONException) {
        e.printStackTrace()
    }

    addToRequestQueue(
        JsonObjectRequest(
            Request.Method.POST,
            AppConstants.WEB_SERVER.toString + AppConstants.SYNC_LOC_DATA.toString, jsonObject,
            SuccessHandler(context, null, success),
            ErrorHandler(context, null, failure)
        )
    )

}

用于从响应中收集结果的处理程序

class SuccessHandler(
val context: Context,
private val progressBar: ProgressBar?,
private val successCallBack: Function<RestCollection, Any>
 ) : Response.Listener<JSONObject> {


override fun onResponse(json: JSONObject?) {
    if (json != null) {
        progressBar?.visibility = View.GONE

        val response: ApplicationResponse =
            RestUtil.getGson().fromJson(json.toString(), ApplicationResponse::class.java)

        successCallBack.apply(response.collection)
    }
}

}

是否有任何解决方法?谢谢!!

我一直在尝试使用Workmanager实现android volley来上传背景数据。实施完全没有问题,但是通过Result ....

android android-volley android-workmanager
1个回答
0
投票

您可以使用作为Volley对象的RequestFuture。它可以帮助您在Worker类中创建同步调用

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