今天主要完成的任務:安卓端接收JSON數據,並構造適配器
遇到的困難:JSON數據格式轉化問題
解決辦法:引入阿里巴巴的json工具包即可
源程序代碼:
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(mContext); recyclerView.setLayoutManager(linearLayoutManager); String url = "http://39.101.190.190:8080/CloudNoteServlet/NoteServlet"; OkHttpUtils .get( ) .url(url) .addParams("method", "getpublicNotes") .build( ) .execute(new StringCallback( ) { @Override public void onError(Call call, Exception e, int id) { Toast.makeText(mContext,"網絡異常", Toast.LENGTH_SHORT).show( ); } @Override public void onResponse(String response, int id) { JSONArray array = JSONArray.parseArray(response); for (int i = 0; i < array.size( ); i++) { JSONObject json = array.getJSONObject(i); Note_pojo notePojo = new Note_pojo(); notePojo.setNote_id(json.getInteger("note_id")); notePojo.setUser_icon(json.getString("user_icon")); notePojo.setUser_name(json.getString("username")); notePojo.setTitle(json.getString("title")); notePojo.setCourse(json.getString("course")); notePojo.setDate(json.getString("date")); notePojoList.add(notePojo); } if(flag) { publicNoteAdapter.refreshList(); swipeRefreshLayout.setRefreshing(false);//當刷新結束時隱藏刷新條 } else { initView(); } } });