使用Android片段中的Volley从url解析json

使用Android片段中的Volley从url解析json

本文介绍了使用Android片段中的Volley从url解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的片段之一中使用 Volley 从下面的 url 解析 json.问题是onResponse"没有激活.我试图用 Log 和 Toast Notification 来捕捉异常.也不例外.似乎 OnResponse 不会在 Fragment 中触发.

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.

这是我的片段.

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;
}

}

这就是我从主活动导航到 Fragment 的方式.

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();
    }

我正在使用 Android Navigation Drawer 基本模板.提前感谢您的帮助.

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

推荐答案

您错过了将请求加入队列的调用.在您的 Fragment 中的 return view 之前 添加此行:

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

rq.add(stringRequest );

这篇关于使用Android片段中的Volley从url解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:16