ListView没有显示JSON数据

时间:2015-09-14 17:50:45

标签: android json

我的listview用于将json数据导入listview,但它没有。 这是我的rooster.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.nijdeken.ccapp.rooster">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Les"
        android:id="@+id/lesson"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Lokaal"
        android:id="@+id/room"
       android:layout_above="@android:id/list"
        android:layout_toLeftOf="@+id/teacher"
        android:layout_toStartOf="@+id/teacher"
        android:layout_marginRight="57dp"
        android:layout_marginEnd="57dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Docent"
        android:id="@+id/teacher"
        android:layout_above="@android:id/list"
        android:layout_toLeftOf="@+id/start"
        android:layout_toStartOf="@+id/start"
        android:layout_marginRight="92dp"
        android:layout_marginEnd="92dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Tijd"
        android:id="@+id/start"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-thin"
        android:id="@android:id/list"
        android:layout_below="@+id/lesson"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

我的Rooster.java文件:

public class rooster extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "http://api.ccapp.it/v2/zportal/schedule/37?token=df679ovlka5urmajmd7tg28lc0";

String apiUrl = "http://api.ccapp.it";

// JSON Node names
private static final String TAG_TIME = "start";
private static final String TAG_ROOM = "locations";
private static final String TAG_TEACHER = "teachers";
private static final String TAG_LESSON = "subjects";

// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rooste);

//    ListView listView = (ListView) findViewById(android.R.id.list);
//    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//    fab.attachToListView(listView);

    contactList = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.lesson))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.room))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.teacher))
                    .getText().toString();

            // Starting single contact activity
//            Intent in = new Intent(getApplicationContext(),
//                  SingleContactActivity.class);
//                in.putExtra(TAG_NAME, name);
//                in.putExtra(TAG_EMAIL, cost);
//                in.putExtra(TAG_PHONE_MOBILE, description);
//                startActivity(in);

        }
    });

    // Calling async task to get json
    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog


    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        Servicehandler sh = new Servicehandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, Servicehandler.GET);

        Log.d("Schedule: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray("");


                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String subject = c.getString(TAG_LESSON);
                    String teachers = c.getString(TAG_TEACHER);
                    String location = c.getString(TAG_ROOM);
                    String start = c.getString(TAG_TIME);


                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_LESSON, subject);
                    contact.put(TAG_TEACHER, teachers);
                    contact.put(TAG_ROOM, location);
                    contact.put(TAG_TIME, start);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                rooster.this, contactList,
                R.layout.list_item, new String[] { TAG_LESSON, TAG_ROOM,
                TAG_TEACHER, TAG_TIME }, new int[] { R.id.lesson,
                R.id.room, R.id.teacher, R.id.time });

        setListAdapter(adapter);
    }

}
public static void getRooster(String appcode, int week){
//        url="http://api.ccapp.it/v2/zportal/schedule/"+week+"?token="+appcode;
    url="http://nijdeken.com/json/schedule.json";
}

}

1 个答案:

答案 0 :(得分:0)

您的JSON网址提供空输出。我尝试在浏览器中然后将值设为空[] ....

请输入您的JSON。 http://api.ccapp.it/v2/zportal/schedule/37?token=df679ovlka5urmajmd7tg28lc0