在获得截击响应后无法在arraylist中设置值

时间:2016-11-22 09:35:41

标签: java android arraylist

//这是我用于计算指定位置和当前位置之间距离的凌空方法,这里我得到了适当的距离。但我不知道为什么距离值没有在ArrayList中设置。

   public void vollyRequest_Fetch_distance(double lat11, double lon11,  double lat22, double lon22,final VolleyCallback callback)

     {
    String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+lat11+","+lon11+"&"+"destinations="+lat22+","+lon22;

    RequestQueue queue = Volley.newRequestQueue(getActivity());

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
    {
        @Override
        public void onResponse(String response)
        {
            callback.onSuccess(response);

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("key", "KEY");
            return params;
        }
    };
    queue.add(request);

}

//此方法用于解析json。

public String Jsonresponse_Distance(String str)
{
    JSONObject jsonObject = null;
    JSONArray jsonArray = null;
    JSONArray jsonArray_elements = null;
    JSONObject jsonObject_elements = null;
    JSONObject jobj;
    String error = null;
    String msg = null;
    try
    {
        jsonObject = new JSONObject(str);
        Log.d("jsonObject==", jsonObject.toString());

        msg = jsonObject.getString("status");
        if (msg.equals("OK"))
        {
                jsonArray = jsonObject.getJSONArray("rows");

                Log.d("jsonArray.length()=",""+jsonArray.length());

            for (int i = 0; i < jsonArray.length(); i++)
                {
                    jobj= jsonArray.getJSONObject(i);
                    jsonArray_elements= jobj.getJSONArray("elements");

                    for (int j = 0; j < jsonArray_elements.length(); j++)
                    {
                        jsonObject_elements= jsonArray_elements.getJSONObject(0);
                        Log.d("jsonObject_elements=",jsonObject_elements.toString());

                        JSONObject job=  jsonObject_elements.getJSONObject("distance");

                        Log.d("job=",job.toString());

                        text= job.getString("text");
                        value= job.getString("value");
                        Log.d("job=",""+text+" "+value);

                    }

                }
        }
        else
        {

        }

    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
        return  value;
}

    public interface VolleyCallback
    {
        void onSuccess(String result);
    }

//我正在调用这个凌空方法,如下所示,在日志中我得到适当的距离值,但无法设置值

salesmanlocation.setDistance();


salesmanlocationArrayList.add(salesmanlocation);

salesmanlocation是我的POJO课程,我试图设置距离。

salesmanlocationArrayList是我的arraylist。

 vollyRequest_Fetch_distance(latt, lng1g, lat, lng, new VolleyCallback()
                    {
                        @Override
                        public void onSuccess(String result)
                        {
                             distance=Jsonresponse_Distance(result);
                             Log.d("distance_vol=","val---== "+distance);
                             salesmanlocation.setDistance(distance);
                        }
                    });

//为了调试目的我这样做了,我在

的日志中得到null
  

salesmanlocationArrayList.get(j)的.getDistance

for(int j=0;j<salesmanlocationArrayList.size();j++)
                {
                    Log.d("salesmanarray_list",""+salesmanlocationArrayList.get(j).getDistanc);

                }

0 个答案:

没有答案