【问题标题】:Parsing json from url with Volley in a Fragment in Android在 Android 的 Fragment 中使用 Volley 从 url 解析 json
【发布时间】:2016-02-27 17:42:36
【问题描述】:

我想在我的 Fragment 之一中使用 Volley 从下面的 url 解析 json。问题是“onResponse”没有激活。我试图用 Log 和 Toast Notification 来捕捉异常。也没有任何例外。似乎 OnResponse 不会在 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;
}

}

这就是我从 Main Activity 导航到 Fragment 的方式。

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 基本模板。提前感谢您的帮助。

【问题讨论】:

    标签: android json android-fragments android-studio android-volley


    【解决方案1】:

    您错过了将请求排队的调用。在您的 Fragment 中 return view 之前添加这一行:

    rq.add(stringRequest );
    

    【讨论】:

    • 非常感谢您的快速回复。它就像一个魅力。
    • 很高兴我能帮上忙 :)
    【解决方案2】:

    您应该编写一个单独的类来发出请求以避免重复代码。此外,您可以减少此类错误。这将是一个很好的编码实践。

    【讨论】:

    • 感谢您的建议。你有任何代码示例要展示吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多