今天主要完成的任务:安卓端接收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(); } } });