【Android新手笔记六】从服务器获取列表

项目需要从服务器获取某个用户的 就诊信息列表。

先定义一个字符串数组用于存放服务器端获得的列表,同时定义一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值