【Android新手笔记六】从服务器获取列表
来源:互联网 发布:淘宝网加绒运动服装 编辑:程序博客网 时间:2024/05/22 01:30
项目需要从服务器获取某个用户的 就诊信息列表。
先定义一个字符串数组用于存放服务器端获得的列表,同时定义一个int数组存放每一项对应的数据表中的主键(编号),方便之后获得该项的具体信息
private String[] strings;private int[] number;进行与服务器的交互
final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("tag", "clinic_reception_list"); jsonObject.put("name", stringname); jsonObject.put("userID", stringID); //数据上传到服务器 } catch (JSONException e) { e.printStackTrace(); } OkHttpClient client = new OkHttpClient(); RequestBody body = FormBody.create(JSON, jsonObject.toString()); Request request = new Request.Builder().url(URL).post(body).build(); Call call=client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG,"error:",e); } @Override public void onResponse(Call call, Response response) throws IOException { //返回的json数据包括jsonarray,详见下文服务端代码 try { JSONObject object = new JSONObject(response.body().string()); times=object.getInt("times"); //列表项的个数 error=object.getBoolean("error"); if (!error) { JSONArray jsonArray = object.getJSONArray("str"); //str对应的value是一个json数组,数组中每个包括列表项和number两个键值对 strings=new String[times]; //声明数组长度 number=new int[times]; for (int i = 0; i < times; i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); strings[i]=jsonObject1.getString("name"); number[i] = jsonObject1.getInt("number"); //列表项和对应number分别填入数组中 } ListView lv = (ListView) findViewById(R.id.listView); lv.setAdapter(new ArrayAdapter<String>(clinic_reception_list.this, android.R .layout.simple_list_item_1, strings)); //strings数组显示在listview上 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(clinic_reception_list.this, clinic_reception_look1.class); Bundle bundle = new Bundle(); bundle.putInt("number", number[position]); //点击了列表中某一项后,将该项对应的number传递给下一个activity并跳转 intent.putExtras(bundle); startActivity(intent); } }); } else { String errorMsg = object.getString("error_msg"); Looper.prepare(); Toast toast = Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG); toast.show(); Looper.loop(); } } catch (JSONException e) { e.printStackTrace(); } } }); }服务器端对应php代码:
else if($tag=='clinic_reception_list'){$name = $data->name; //data是接收Android发送的json数据$userID = $data->userID;$str=array();$dbname = "test"; // 数据库名 $select = mysqli_select_db($conn, $dbname); // 选择数据库$charset= mysqli_set_charset($conn,"utf-8"); //编码格式$result = mysqli_query($conn,"SELECT * FROM clinic_reception WHERE userID = $userID"); //查找接诊表格中该用户就诊信息if(!$result){$response["error"]=true;$response["error_msg"] ="还没有就诊信息";$response =json_encode($response); echo $response;}else{$response["error"]=false;$response["times"]=mysqli_num_rows($result);$i=0;while($row = mysqli_fetch_array($result)){ $str[$i]["name"]="接诊记录表".$row["reception_date"]; //该用户的所有接诊记录表记录以接诊记录表+日期的形式返回 $str[$i]["number"]=$row["Number"]; //接诊记录表的主键也response,用于根据主键获取详细的接诊记录表内容$i++; }$response["str"]=($str);$response =json_encode($response); echo $response;}}
0 0
- 【Android新手笔记六】从服务器获取列表
- Android笔记 Android客户端从服务器获取源码乱码demo
- android从服务器获取图片
- 一个新手从零搭建服务器的笔记教程
- 手机APP从服务器获取列表和详情
- Android如何从服务器获取图片
- Android如何从服务器获取图片
- Android 从网络服务器获取图片
- android从服务器获取apk安装
- Android从ftp服务器获取文件
- 获取DNS服务器列表
- Android 按照日期从旧到新获取目录下文件列表
- Android连接服务器,从服务器获取数据,以及从服务器下载文件(单,多线程)
- Android学习笔记---- HttpClient获取服务器数据
- 从服务器填充一个列表
- 从服务器获取数据
- 从服务器获取json
- android 学习笔记之六 动态获取权限
- UVA oj 489 Hangman Judge(字符串)
- 简单的分布式图片存储及搜索访问服务
- 面向对象程序设计-继承与多态
- 基于IJKDanmakuFlameMaster的android 弹幕接入
- iOS百度地图简单应用( iOS地图定位(定位、地理编码与反地理编码、mapView、大头针)
- 【Android新手笔记六】从服务器获取列表
- Android BuildConfig.DEBUG的使用详解
- vagrant create a base box
- Mysql的基本优化
- Linux学习要点
- CnSkype信息互联办公对企业的影响有多大
- moveTaskToback退后台的用法及作用
- iOS 基础篇9—ios应用数据存储方式(XML属性列表-plist)
- 数据泵的使用