Parsing json from url with Volley in a Fragment in Android

只谈情不闲聊 提交于 2020-01-01 19:57:06

问题


I want to parse json from the url below with Volley in one of my Fragment. The problem is that the "onResponse" does not activated. I tried to catch exception with Log and with Toast Notification. No any exception as well. It seems OnResponse doesnt fire in Fragment.

This is my Fragment.

public class PlacesToVisitFragment extends Fragment {

public PlacesToVisitFragment() {
    // Required empty public constructor
}

StringBuilder sb = new StringBuilder();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_places_to_visit, container, false);

    final TextView txt=  (TextView)view.findViewById(R.id.textView8);

    RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());

    String url= "http://www.flightradar24.com/AirportInfoService.php?airport=ORY&type=in";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Do something with the response

                    try{

                        JSONObject o = new JSONObject(response);
                        JSONArray values=o.getJSONArray("flights");

                        for ( int i=0; i< values.length(); i++) {

                            JSONObject sonuc = values.getJSONObject(i);

                            sb.append("Flight: " + sonuc.getString("flight") + "\n");
                            sb.append("name: " + sonuc.getString("name") + "\n\n");

                        }

                        txt.setText(sb.toString());


                    }  catch (JSONException ex){}

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



    // Inflate the layout for this fragment
    return  view;
}

}

This is how I navigate to the Fragment from Main Activity.

if (id == R.id.nav_camera) {
        PlacesToVisitFragment fragment = new PlacesToVisitFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.mainFrame, fragment);
        ft.addToBackStack(null);
        ft.commit();
    }

I am using Android Navigation Drawer basic template. Thanks for your help in advance.


回答1:


You are missing the call to en-queue the request. Add this line before the return view in your Fragment :

rq.add(stringRequest );



回答2:


You should write a separate class for making a request to avoid duplicate code. Also, you can reduce such mistakes. It will be a good coding practice.



来源:https://stackoverflow.com/questions/35673159/parsing-json-from-url-with-volley-in-a-fragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!