总计小时数无法从android插入MySQL [英] Total hours cannot be inserted into MySQL from android

查看:72
本文介绍了总计小时数无法从android插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的公式获得总小时数:

I get the total hours by using the formula below:

 public void updateTotalHours()
    {
        int a = SplitTime(objMyCustomBaseAdapter.getFistTime());
        int b = SplitTime(objMyCustomBaseAdapter.getLastTime());
        long difference = 0;
        if (a > b) {
            difference = (b + (24 * 60) - a) - (1 * 60);
            int minutes = (int) (difference % 60);
            int hours = (int) ((difference / 60) % (24 * 60));
            totalHours.setText(("Total hours : " + hours + ":" + minutes));

        } else {
            difference = (b - a) - (1 * 60);
            int minutes = (int) (difference % 60);
            int hours = (int) ((difference / 60) % (24 * 60));
            totalHours.setText(("Total hours : " + hours + ":" + minutes));
        }
    }
        }

获得后totalHours ,我试图将其转换为 String 并保存到 MySQL

After get the totalHours, I trying to convert it into String and save into MySQL

     TotalHours=totalHours.getText().toString();
      String[] time= TotalHours.split(":", 2);
   Toast.makeText(getApplicationContext(),time[1].trim(),Toast.LENGTH_LONG).show();
      addInformation(name, weather, date2, status, first1[1], last1[1],time[1]);

addInformation

 public class AddInfo extends AsyncTask<String, Void, ContentValues> {

        private final Context context;
        ProgressDialog loading;

        public AddInfo(Context context) {
            this.context = context;
        }

        private ContentValues handleActionSend(String name, String weather, String date2, String status, String timeIn, String timeOut, String hours) {
            ContentValues retour = new ContentValues();
            retour.put("success", false);

            try {
                HttpPost post = new HttpPost(Configs.ADD_INFORMATION);
                post.addHeader("Accept", "application/json");
                List<NameValuePair> postParams = new ArrayList<NameValuePair>();
                postParams.add(new BasicNameValuePair(Configs.KEY_USER_NAME, name));
                postParams.add(new BasicNameValuePair(Configs.KEY_WEATHER, weather));
                postParams.add(new BasicNameValuePair(Configs.KEY_DATE, date2));
                postParams.add(new BasicNameValuePair(Configs.KEY_STATUS, status));
                postParams.add(new BasicNameValuePair(Configs.KEY_TIMEIN, timeIn));
                postParams.add(new BasicNameValuePair(Configs.KEY_TIMEOUT, timeOut));
                postParams.add(new BasicNameValuePair(Configs.KEY_TOTALHOURS,hours));

                post.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
                HttpClient client = new DefaultHttpClient();

                HttpResponse response = client.execute(post);

                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                StringBuilder result = new StringBuilder();
                String line;
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }

                JSONObject json = new JSONObject(result.toString());
                retour.put("success", true);
                retour.put("lastId", json.getInt("lastId"));
                //Log.e("AA",String.valueOf(json.getInt("lastId")));
            } catch (MalformedURLException e) {
                retour.put("message", "MalformedURLException Error: " + e.getLocalizedMessage());
            } catch (UnsupportedEncodingException e) {
                retour.put("message", "UnsupportedEncodingException Error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                retour.put("message", "IOException Error: " + e.getLocalizedMessage());
            } catch (Exception e) {
                retour.put("message", "Exception Error: " + e.getLocalizedMessage());
            }
            return retour;
        }


        @Override
        protected ContentValues doInBackground(String... args) {

            //loading = ProgressDialog.show(context, "Please Wait", null, true, true);
            return handleActionSend(args[0], args[1], args[2], args[3], args[4], args[5],args[6]);
        }

        @Override
        protected void onPreExecute() {
            loading = ProgressDialog.show(context, "Please Wait", null, true, true);
        }

        @Override
        protected void onPostExecute(ContentValues contentValues) {
            super.onPostExecute(contentValues);
            if (contentValues != null) {
                loading.dismiss();
                String message;
                if (contentValues.getAsBoolean("success")) {
                    message = "Inserted successfully: " + contentValues.getAsString("lastId");
                    lastID = contentValues.getAsString("lastId");
                    Toast.makeText(getApplicationContext(), "D" + lastID, Toast.LENGTH_LONG).show();
                } else {
                    message = "An errorr has occured!\n" + contentValues.getAsString("message");
                }
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "No internet connection. Please try again", Toast.LENGTH_LONG).show();
            }
        }

    }

    public void addInformation(final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut, final String hours) {
        AddInfo ru = new AddInfo(this);
        ru.execute(name, weather, date2, status, timeIn, timeOut,hours);
    }

addInformation.php

<?php 
require_once('dbConnect.php');
    if($_SERVER['REQUEST_METHOD']=='POST'){

        $response = array();

        //Getting values

        $name = $_POST['name'];
        $weather = $_POST['weather'];
        $date = $_POST['date'];
        $status = $_POST['status'];
        $timeIn = $_POST['timeIn'];
        $timeOut = $_POST['timeOut'];
        $hours = $_POST['totalHours'];

        print_r($hours);
        //Creating an sql query
        $sql = "INSERT INTO information(name, weather, date, status, time_in, time_out, total_hours) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut', '$hours')";

        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if(mysqli_query($con,$sql)){
            //echo 'Information Added Successfully';
            $insertId= mysqli_insert_id($con);
            $response["lastId"]=$insertId;
            echo json_encode($response);
        }else{
            echo 'Could Not Add Information';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

所有数据都可以插入MySQL,只需要total_hours总是0.我不知道是不是错误来自这条线?

All the data can be inserted into MySQL, just the total_hours is always 0. I wonder is it the error came from this line ?

 TotalHours=totalHours.getText().toString();




发生错误!异常错误:值< br 类型
java.lang.String无法转换为JSONObject。

An Error has occured!Exception Error: Value <br of type java.lang.String cannot be converted to JSONObject.


推荐答案

在这里和那里搜索后,我重新创建表并将名称更改为小时...之后,它可以正常工作

After searching here and there, I recreate the table and change the column named to hours...After that, it works fine

这篇关于总计小时数无法从android插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆