douwen5584 2017-11-04 00:05
浏览 49

Android Php Mysql日期范围搜索

Can anyone help me? I'm developing an android app in android studio which will implement a Date Range search. the user will select the between date the "FROM" date and the "TO" date then the result will filter in a recyclerview. I'm using php mysql as our query. Hoping for a good and better reply. Thanks in advance

private class FilterDate extends AsyncTask {

    ProgressDialog pdLoading = new ProgressDialog(Complaint.this);
    HttpURLConnection conn;
    URL url = null;
    String from;
    String to;



    public FilterDate(String from,String to) {
        this.from = from;
        this.to = to;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();
    }


    @Override
    protected String doInBackground(String... params) {
        try {

            // Enter URL address where your php file resides
            url = new URL("http://192.168.0.100/Android/filter.php");

        } catch (MalformedURLException e) {
            e.printStackTrace();
            // TODO Auto-generated catch block
            return e.toString();
        }
        try {

            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");

            // setDoInput and setDoOutput to true as we send and recieve data
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.connect();

            // add parameter to our above url
            Uri.Builder builder1 = new Uri.Builder().appendQueryParameter("from", from);
            String query1 = builder1.build().getEncodedQuery();

            OutputStream os1 = conn.getOutputStream();
            BufferedWriter writer1 = new BufferedWriter(new OutputStreamWriter(os1, "UTF-8"));
            writer1.write(query1);
            writer1.flush();
            writer1.close();
            os1.close();
            conn.connect();

            Uri.Builder builder2 = new Uri.Builder().appendQueryParameter("to", from);
            String query2 = builder2.build().getEncodedQuery();

            OutputStream os2 = conn.getOutputStream();
            BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(os1, "UTF-8"));
            writer2.write(query2);
            writer2.flush();
            writer2.close();
            os2.close();
            conn.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {
            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {


                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {
                return ("Connection error");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        //this method will be running on UI thread suggestion
        ArrayList<String> dataList = new ArrayList<String>();
        pdLoading.dismiss();

        //   pdLoading.dismiss();
        List<DataComplaints> data = new ArrayList<>();
        pdLoading.dismiss();
        if (result.equals("no rows")) {
            Toast.makeText(Complaint.this, "No Results found for entered query", Toast.LENGTH_SHORT).show();
        } else {

            try {

                JSONArray jArray = new JSONArray(result);

                // Extract data from json and store into ArrayList as class objects
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    DataComplaints healthData = new DataComplaints();


                    healthData.idNum = json_data.getString("student_number");
                    healthData.lastName = json_data.getString("lastname");
                    healthData.firstName = json_data.getString("firstname");
                    healthData.middleName = json_data.getString("middlename");
                    healthData.complaint = json_data.getString("common_type");
                    healthData.medicine = json_data.getString("medicinetype");
                    healthData.date = json_data.getString("date");
                    healthData.time = json_data.getString("time");

                    data.add(healthData);
                    dataList.add(json_data.getString("date"));
                }

                strArrData = dataList.toArray(new String[dataList.size()]);

                // Setup and Handover data to recyclerview
                mRVFish = (RecyclerView) findViewById(R.id.studentList);
                mAdapter = new AdapterComplaints(Complaint.this, data);
                mRVFish.setAdapter(mAdapter);
                mRVFish.setLayoutManager(new LinearLayoutManager(Complaint.this));

            } catch (JSONException e) {
                // You to understand what actually error is and handle it appropriately
                Toast.makeText(Complaint.this, "Error in Fetching Data", Toast.LENGTH_LONG).show();
                Toast.makeText(Complaint.this, "Error!", Toast.LENGTH_LONG).show();
            }
        }
    }
}

This is my query in php:

<?php
$host='127.0.0.1';
$username='root';
$pwd='';
$db="cjc_clinic";
$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');
if(mysqli_connect_error($con))
{
  echo "Failed to Connect to Database ".mysqli_connect_error();
}
$From=$_POST['from'];
$To=$_POST['to'];
//$sql="SELECT * from studentcomplaint_view where lastname LIKE '%$name%'";
//$sql="SELECT * FROM studentcomplaint_view  where CONCAT(lastname,' ',firstname) LIKE '%$name%'";

//$sql="SELECT * FROM studentcomplaint_view WHERE Date >= '$From' AND Date <='$To'";
$sql="SELECT * FROM studentcomplaint_view WHERE Date BETWEEN '$From' AND '$To'";

$query=mysqli_query($con,$sql);

if($query){

    while($row=mysqli_fetch_array($query))
  {
    $data[]=$row;
  }
    print(json_encode($data));
}else
{
  echo('Not Found ');
}
mysqli_close($con);
?>
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大