Android获取json并形成一个列表

时间:2017-07-20 08:30:58

标签: android

我是一个新的Android用户。我有一个带有php脚本的mysql数据库,它返回一个json http://localhost/FindIt/all_items.php

    [  
       {  
          "id":"1",
          "name":"Mukisa Ivan",
          "title":"Makerere University ID",
          "description":"This is a lost a ID picked at night near Olympia Hostel Gate",
          "date":"2017-05-01",
          "reward":"10,000",
          "location":"Makerere Kikoni",
          "photo":"",
          "category":"Id",
          "type":"Found",
          "userId":"1",
          "email":"mukisa@gmail.com",
          "phone":"+256706330511",
          "userType":"Founder"
       },
       {  
          "id":"3",
          "name":"Kalule Tom",
          "title":"UACE Results Slip",
          "description":"Lost My UACE results slip in areas of Wandegeya",
          "date":"2017-03-07",
          "reward":"5,000",
          "location":"Wandegeya",
          "photo":"",
          "category":"Document",
          "type":"Lost",
          "userId":"2",
          "email":"ktom@gmail.com",
          "phone":"+256706330511",
          "userType":"Loser"
       }
    ]

我想获取上述数据并在片段中形成一个列表,如下所示: -

    package com.example.kenneth.lostfound;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class Fragment2 extends Fragment {

        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @return A new instance of fragment Fragment2.
         */
        public static Fragment2 newInstance() {
            return new Fragment2();
        }

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

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }

        @Override
        public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
            inflater.inflate(R.menu.fragment2, menu);
        }

        @Override
        public boolean onOptionsItemSelected (MenuItem item){
            int id = item.getItemId();
            if (id == R.id.action_refresh){
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_fragment2, container, false);

            String[] lostItemArray = {
                    "National ID - UCU Main Gate - 20/2/2016",
                    "UACE Results slip - Kampala Rd Taxi - 23/2/2016",
                    "Driving Permit - Ham Towers Makerere- 10/2/2016",

            };

            List<String> lost_items = new ArrayList<String>(
                    Arrays.asList(lostItemArray)
            );

            ArrayAdapter<String> mLostFoundAdapter = new ArrayAdapter<String>(
                    getActivity(),
                    R.layout.list_item_lostfound,
                    R.id.list_item_lostfound,
                    lost_items
            );

            ListView listView = (ListView) view.findViewById(
                    R.id.listview_lostfound
            );
            listView.setAdapter(mLostFoundAdapter);

            return view;
        }
    }

我想从json获取数据来更新列表,或者在列表View元素中填充它。我正在使用android studio 2.3

1 个答案:

答案 0 :(得分:0)

您可以使用VolleyRetrofit等任何网络库从服务器获取json响应。

在此,我提供了凌空样本和改造样本的链接。

  1. 排球

    Volley sample 1

    Volley library to handle to webservice

  2. 改造

    Retrofit sample

  3. 来json解析也有一些库用于快速解析Logan Squaregson

    1. Logan Square

      2。Gson

    2. 希望这有用:)