我想从一个函数调用 volley 请求,该函数将 return arraylist 到其他一些 activity 并且 volley 响应需要转换为模型
I want to call a volley request from a function that will return the arraylist to some other activity and volley response needs to converted to model
我想从 volley 中获取结果,然后解析结果以创建模型 arraylist 和调用 volley 请求的函数应该 return arraylist,该函数从各种其他活动和代码中调用是别人写的,所以我需要想办法在这个 class
中实现同样的目标
这是完整的 class 代码
class Mock @Inject constructor(private val context: Context) {
val url = Constant.prefix + "get_short_videos.php"
fun loadMockData(): ArrayList<StoriesDataModel>? {
// val mockData = context.resources.openRawResource(R.raw.stories_data)
// val dataString = mockData.bufferedReader().readText()
val scope = CoroutineScope(Dispatchers.Main).launch {
val data = getData()
}
Log.e("Rresponse",dataString+"-");
val gson = Gson()
val storiesType = object : TypeToken<ArrayList<StoriesDataModel>>() {}.type
val storiesDataModelList = gson.fromJson<ArrayList<StoriesDataModel>>(dataString, storiesType)
return storiesDataModelList
}
suspend fun getData() = suspendCoroutine<String> { cont ->
val requestQueue = Volley.newRequestQueue(context)
val postRequest: StringRequest = object : StringRequest(
Method.POST, url,
Response.Listener { response: String? ->
Log.e("response",response.toString());
cont.resume(response.toString())
},
Response.ErrorListener { error: VolleyError ->
error.printStackTrace()
Toast.makeText(context, "Check your internet connection", Toast.LENGTH_SHORT)
.show()
}
) {
override fun getParams(): Map<String, String>? {
val params: MutableMap<String, String> = HashMap()
params["mobile"] = context.getSharedPreferences(Constant.prefs, Context.MODE_PRIVATE).getString("mobile","").toString();
params["length"] = "0";
return params
}
}
postRequest.retryPolicy =
DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
requestQueue.add(postRequest)
}
}
我尝试了互联网上提供的各种不同方法,但无法找到一种方法,kotlin 的新手,所以对协程不太了解,感谢任何帮助,提前致谢
我添加的这些 suspendCoroutine 和 CoroutineScope 所以这些可能不应该在这里
能够找到解决问题的方法,如果有人需要这个,这里是答案
class Mock @Inject constructor(private val context: Context) {
val url = Constant.prefix + "get_short_videos.php"
private var parentJob = Job()
private val coroutineContext: CoroutineContext get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
suspend fun loadMockData(): ArrayList<StoriesDataModel>? {
// val mockData = context.resources.openRawResource(R.raw.stories_data)
// val dataString = mockData.bufferedReader().readText()
var dataString = "";
// val scope = CoroutineScope(Dispatchers.Main).launch {
// dataString = getData()
// }
scope.async(Dispatchers.IO) {
val success = async { getData() }
dataString = success.await()
Log.e("Rresponse2",dataString+"-");
}.await();
Log.e("Rresponse",dataString+"-");
val gson = Gson()
val storiesType = object : TypeToken<ArrayList<StoriesDataModel>>() {}.type
val storiesDataModelList = gson.fromJson<ArrayList<StoriesDataModel>>(dataString, storiesType)
return storiesDataModelList
}
suspend fun getData() = suspendCoroutine<String> { cont ->
val requestQueue = Volley.newRequestQueue(context)
val postRequest: StringRequest = object : StringRequest(
Method.POST, url,
Response.Listener { response: String? ->
Log.e("response",response.toString());
cont.resume(response.toString())
},
Response.ErrorListener { error: VolleyError ->
error.printStackTrace()
Toast.makeText(context, "Check your internet connection", Toast.LENGTH_SHORT)
.show()
}
) {
override fun getParams(): Map<String, String>? {
val params: MutableMap<String, String> = HashMap()
params["mobile"] = context.getSharedPreferences(Constant.prefs, Context.MODE_PRIVATE).getString("mobile","").toString();
params["length"] = "0";
return params
}
}
postRequest.retryPolicy =
DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
requestQueue.add(postRequest)
}
}
还需要使函数从调用的主函数挂起
我想从 volley 中获取结果,然后解析结果以创建模型 arraylist 和调用 volley 请求的函数应该 return arraylist,该函数从各种其他活动和代码中调用是别人写的,所以我需要想办法在这个 class
中实现同样的目标这是完整的 class 代码
class Mock @Inject constructor(private val context: Context) {
val url = Constant.prefix + "get_short_videos.php"
fun loadMockData(): ArrayList<StoriesDataModel>? {
// val mockData = context.resources.openRawResource(R.raw.stories_data)
// val dataString = mockData.bufferedReader().readText()
val scope = CoroutineScope(Dispatchers.Main).launch {
val data = getData()
}
Log.e("Rresponse",dataString+"-");
val gson = Gson()
val storiesType = object : TypeToken<ArrayList<StoriesDataModel>>() {}.type
val storiesDataModelList = gson.fromJson<ArrayList<StoriesDataModel>>(dataString, storiesType)
return storiesDataModelList
}
suspend fun getData() = suspendCoroutine<String> { cont ->
val requestQueue = Volley.newRequestQueue(context)
val postRequest: StringRequest = object : StringRequest(
Method.POST, url,
Response.Listener { response: String? ->
Log.e("response",response.toString());
cont.resume(response.toString())
},
Response.ErrorListener { error: VolleyError ->
error.printStackTrace()
Toast.makeText(context, "Check your internet connection", Toast.LENGTH_SHORT)
.show()
}
) {
override fun getParams(): Map<String, String>? {
val params: MutableMap<String, String> = HashMap()
params["mobile"] = context.getSharedPreferences(Constant.prefs, Context.MODE_PRIVATE).getString("mobile","").toString();
params["length"] = "0";
return params
}
}
postRequest.retryPolicy =
DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
requestQueue.add(postRequest)
}
}
我尝试了互联网上提供的各种不同方法,但无法找到一种方法,kotlin 的新手,所以对协程不太了解,感谢任何帮助,提前致谢
我添加的这些 suspendCoroutine 和 CoroutineScope 所以这些可能不应该在这里
能够找到解决问题的方法,如果有人需要这个,这里是答案
class Mock @Inject constructor(private val context: Context) {
val url = Constant.prefix + "get_short_videos.php"
private var parentJob = Job()
private val coroutineContext: CoroutineContext get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
suspend fun loadMockData(): ArrayList<StoriesDataModel>? {
// val mockData = context.resources.openRawResource(R.raw.stories_data)
// val dataString = mockData.bufferedReader().readText()
var dataString = "";
// val scope = CoroutineScope(Dispatchers.Main).launch {
// dataString = getData()
// }
scope.async(Dispatchers.IO) {
val success = async { getData() }
dataString = success.await()
Log.e("Rresponse2",dataString+"-");
}.await();
Log.e("Rresponse",dataString+"-");
val gson = Gson()
val storiesType = object : TypeToken<ArrayList<StoriesDataModel>>() {}.type
val storiesDataModelList = gson.fromJson<ArrayList<StoriesDataModel>>(dataString, storiesType)
return storiesDataModelList
}
suspend fun getData() = suspendCoroutine<String> { cont ->
val requestQueue = Volley.newRequestQueue(context)
val postRequest: StringRequest = object : StringRequest(
Method.POST, url,
Response.Listener { response: String? ->
Log.e("response",response.toString());
cont.resume(response.toString())
},
Response.ErrorListener { error: VolleyError ->
error.printStackTrace()
Toast.makeText(context, "Check your internet connection", Toast.LENGTH_SHORT)
.show()
}
) {
override fun getParams(): Map<String, String>? {
val params: MutableMap<String, String> = HashMap()
params["mobile"] = context.getSharedPreferences(Constant.prefs, Context.MODE_PRIVATE).getString("mobile","").toString();
params["length"] = "0";
return params
}
}
postRequest.retryPolicy =
DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
requestQueue.add(postRequest)
}
}
还需要使函数从调用的主函数挂起