没有从JSON调用Android获取数据

时间:2015-10-01 13:18:55

标签: android json

这是我的java文件,我试图调用Json,但我没有得到任何数据。此外,我没有收到任何错误,所以我找不到问题所在。

这是我的代码,Json看起来像这样:

[
  {
    "title": "Quest",
    "description": "Description Quest",
    "district": "District Quest"; 
  }
]

和代码:

    public class Quests extends Fragment {

    // Log tag
    private static final String TAG = Quests.class.getSimpleName();

    // Quest Json url
    private static final String url = "http://my-ip-adress-of-computer/project/quests.txt";
    private ProgressDialog pDialog;
    private List<com.dusandimitrijevic.model.Quests> questList = new ArrayList<com.dusandimitrijevic.model.Quests>();
    private ListView listView;
    private QuestListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.quests, container, false);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.show();

        listView = (ListView) rootView.findViewById(R.id.list);
        adapter = new QuestListAdapter(getActivity(), questList);
        listView.setAdapter(adapter);

        fetchQuests();


        return rootView;
    }

    private void fetchQuests() {
        // Creating volley request obj
        JsonArrayRequest questReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                com.dusandimitrijevic.model.Quests q = new com.dusandimitrijevic.model.Quests();
                                q.setTitle(obj.getString("title"));
                                q.setDescription(obj.getString("description"));
                                q.setDistrict(obj.getString("district"));

                                // adding quests to quest array
                                questList.add(q);

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

                        }
                        adapter.notifyDataSetChanged();
                        // stopping swipe refresh
                        //swipeRefreshLayout.setRefreshing(false);
                    }


                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

                // stopping swipe refresh
                //swipeRefreshLayout.setRefreshing(false);
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(questReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

分号;不是有效的字段分隔符。您也不能在最后一个字段后使用字段分隔符。有效的JSON应如下所示:

[
  {
    "title": "Quest",
    "description": "Description Quest",
    "district": "District Quest"
  }
]