This question already has an answer here:
How can I return value from function onResponse of Volley?
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
>  public  void makeJsonObjectRequest(Long s,Double lat, Double lon, int
> timezone) {
>         String urlJsonObj = "http://projet.dev.e-njaz.com/api/example/getPrayerTimes/" + s + "/" +
> lat + "/" + lon + "/" + timezone;
>         JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
>                 urlJsonObj, null, new Response.Listener<JSONObject>() {
>             @Override
>             public void onResponse(JSONObject response) {
>                 Log.d(TAG, response.toString());
>                 try {
>                     DateFormat i=new SimpleDateFormat("HH:mm");
>                     DateFormat o = new SimpleDateFormat("KK:mm a");
>                     prayertimes.setStr_asarr(response.toString());
>                     prayertimes.str_ishaa=o.format(i.parse(response.getString("6").toString()));
>                 } catch (JSONException e) {
>                     e.printStackTrace();
>                 } catch (ParseException e) {
>                     e.printStackTrace();
>                 }
>             }
>
>         }, new Response.ErrorListener() {
>             @Override
>             public void onErrorResponse(VolleyError error) {
>                 VolleyLog.d(TAG, "Error: " + error.getMessage());
>                 Toast.makeText(getApplicationContext(),
>                         error.getMessage(), Toast.LENGTH_SHORT).show();
>             }
>         });
>         AppController.getInstance().addToRequestQueue(jsonObjReq);
>
>     }


您好evrybody,我需要在onReponse函数中返回对象付款人的时间,您能帮我吗?当我更改onReponse的类型返回值时,出现错误。

最佳答案

您必须使用“回调”从截击中返回值。这是一个相当复杂的方法,所以我建议一种替代方法。您应该只在活动中创建一个变量,然后在onResponse中设置其值,然后在所需的任何函数中使用该变量(甚至可以从onResponse中调用该函数)。

如果您仍想尝试返回值,请检查this

如果您不明白,请在评论中保留

10-07 19:25
查看更多