从Android向phpmyadmin

时间:2019-05-21 12:54:51

标签: java php android

我是Android Studio中的新手编程,在为phpmyadmin中的db中输入在android中输入的数据之前,我正在寻找对此代码的解决方案,但是现在我不知道错误在哪里,唯一的问题是确实是在php文件中添加了更多字段。到目前为止,我的工作是:

Main2Activity.java

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.Calendar;
import java.util.Objects;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener, Response.Listener<JSONObject>, Response.ErrorListener   {

    private static final String TAG = "Main2Activity";
    private EditText nd, date, dbruto, veintiuno, subt1, descuento5, subt2,  descuento10, descuento15, subt3, descuento2, subt4,  descuento1, tdescuento, dtotal;
    private RequestQueue rq;
    private JsonRequest jrq;
    private Button rdiezmo;

    private DatePickerDialog.OnDateSetListener nDateSetListener;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        rq = Volley.newRequestQueue(this);
        rdiezmo = findViewById(R.id.btnrd);
        date=findViewById(R.id.txtdate);
        date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Calendar calendar=Calendar.getInstance();
                int year=calendar.get(Calendar.YEAR);
                int month=calendar.get(Calendar.MONTH);
                int day=calendar.get (Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(Main2Activity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        nDateSetListener,
                        year,month,day);
                Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });
        nDateSetListener = new
                DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        month = month +1;
                        Log.d(TAG,"onDateSet: mm/dd/yyy: "+ year +"-"+month+"-"+day);
                        String ndate = year + "-"+month+"-"+day;
                        date.setText(ndate);
                    }
                };

        TextView txtBienvenido = findViewById(R.id.txtbienvenido);
        txtBienvenido.setText("¡Por favor! Ingrese los datos");
        nd = findViewById(R.id.txtndom);
        dbruto = findViewById(R.id.txtdbruto);
        String dato = getIntent().getStringExtra("dato");
        dbruto.setText(dato);
        veintiuno = findViewById(R.id.txtd21);
        subt1 = findViewById(R.id.txts1);
        descuento5 = findViewById(R.id.desc5);
        subt2 = findViewById(R.id.txts2);
        descuento10 = findViewById(R.id.desc10);
        descuento15 = findViewById(R.id.desc15);
        subt3 = findViewById(R.id.txts3);
        descuento2 = findViewById(R.id.desc2);
        subt4 = findViewById(R.id.txts4);
        descuento1 = findViewById(R.id.desc1);
        tdescuento = findViewById(R.id.txtdesc);
        dtotal = findViewById(R.id.txtdiezmob);
        findViewById(R.id.btncd).setOnClickListener(this);
        findViewById(R.id.btnrd).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btncd:
                //Calcula descuento 21%
                float v21 = Float.parseFloat((dbruto.getText().toString()));
                float d21 = (v21 * 21) / 100;
                String r1 = String.valueOf((double) d21);
                veintiuno.setText((r1));

                //Calcula subtotal 1
                float s1 = v21 - d21;
                String r2 = String.valueOf((double) s1);
                subt1.setText((r2));

                //Calcula descuento 5%
                float d5 = (s1 * 5) / 100;
                String r3 = String.valueOf((double) d5);
                descuento5.setText((r3));

                //Calcula subtotal 2
                float s2 = s1 - d5;
                String r4 = String.valueOf((double) s2);
                subt2.setText((r4));

                //Como se está calculando el 5% este valor es 0
                float d10 = 0;
                String r5 = String.valueOf((double) d10);
                descuento10.setText((r5));

                //Como se está calculando el 5% este valor es 0
                float d15 = 0;
                String r6 = String.valueOf((double) d15);
                descuento15.setText((r6));

                //Como se está calculando el 5% este valor es 0
                float s3 = 0;
                String r7 = String.valueOf((double) s3);
                subt3.setText((r7));

                //Calcula descuento 2%
                float d2 = (s2 * 2) / 100;
                String r8 = String.valueOf((double) d2);
                descuento2.setText((r8));

                //Calcula subtotal 4
                float s4 = s2 - d2;
                String r9 = String.valueOf((double) s4);
                subt4.setText((r9));


                //Calcula descuento 1%
                float d1 = (s4 * 1) / 100;
                String r10 = String.valueOf((double) d1);
                descuento1.setText((r10));

                //Calcula total descuento
                float tdesc = d21 + d5 + d2 + d1;
                String r11 = String.valueOf((double) tdesc);
                tdescuento.setText((r11));

                //Calcula diezmo neto
                float dneto = s4 - d1;
                String r12 = String.valueOf((double) dneto);
                dtotal.setText((r12));

                break;

            case R.id.btnrd:

                String url =
                        "https://ipucsegundaalgarrobo.000webhostapp.com/diezmo15.php?nd=" + nd.getText().toString() + "&fd=" + date.getText().toString() +
                                "&db=" + dbruto.getText().toString() + "&sb1=" + dbruto.getText().toString() + "&d1=" + veintiuno.getText().toString() +
                                "&sb2=" + subt1.getText().toString() + "&d2=" + descuento5.getText().toString() + "&sb3=" + subt2.getText().toString() +
                                "&d3=" + descuento10.getText().toString() + "&d4=" + descuento15.getText().toString() + "&sb4=" + subt3.getText().toString() +
                                "&d5=" + descuento2.getText().toString() + "&sb5=" + subt4.getText().toString() + "&d6=" + descuento1.getText().toString() +
                                "&td=" + tdescuento.getText().toString() + "&dn=" + dtotal.getText().toString();

                jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
                rq.add(jrq);
                Log.i(" URL ", " Url de petición " +url);
                break;
        }
    }
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(this, "No se pudo registrar los datos" + error.toString(),Toast.LENGTH_LONG).show();
    }
    @Override
    public void onResponse(JSONObject response) {
        Toast.makeText(this, "Se ha registrado los datos correctamente",Toast.LENGTH_SHORT).show();
        limpiarRegistros();
    }
    private void limpiarRegistros() {
        nd.setText("");
        date.setText("");
        dbruto.setText("");
        veintiuno.setText("");
        subt1.setText("");
        descuento5.setText("");
        subt2.setText("");
        descuento10.setText("");
        descuento15.setText("");
        descuento2.setText("");
        subt3.setText("");
        subt4.setText("");
        descuento1.setText("");
        tdescuento.setText("");
        dtotal.setText("");
    }
}

diezmo15.php文件是这样的:

<?php

$json=array();
    if(isset($_GET["nd"]) && ($_GET["fd"]) &&($_GET["db"]) && ($_GET["sb1"]) && ($_GET["d1"]) && ($_GET["sb2"]) && ($_GET["d2"]) && ($_GET["sb3"]) && ($_GET["d3"]) && ($_GET["d4"]) && ($_GET["sb4"]) && ($_GET["d5"]) && ($_GET["sb5"]) && ($_GET["d6"]) && ($_GET["td"]) && isset($_GET["dn"])){
        $nd = $_GET['nd'];
        $fd = $_GET['fd'];
        $db = $_GET['db'];
        $sb1 = $_GET['sb1'];
        $d1 = $_GET['d1'];
        $sb2 = $_GET['sb2'];
        $d2 = $_GET['d2'];
        $sb3 = $_GET['sb3'];
        $d3 = $_GET['d3'];
        $d4 = $_GET['d4'];
        $sb4 = $_GET['sb4'];
        $d5 = $_GET['d5'];
        $sb5 = $_GET['sb5'];
        $d6 = $_GET['d6'];
        $td = $_GET['td'];
        $dn = $_GET['dn'];

$conexion = mysqli_connect 
("localhost","tubd","tucontrasena","tutabla" ) or die  
("Sin Conexion");

        $consulta="INSERT INTO diezmos15(ndom, fdom, dbru, subt1,  
desc21, subt2, desc5, subt3, desc10, desc15, subt4, desc2, subt5, desc1, tdesc, dneto) 

VALUES  
('{$nd}','{$fd}','{$db}','{$sb1}','{$d1}','{$sb2}','{$d2}','{$sb3}','{$d3}','{$d4}','{$sb4}','{$d5}','{$sb5}','{$d6}','{$td}','{$dn}')";
        $resultado=mysqli_query($conexion,$consulta);
        if($consulta){
           $consulta="SELECT * FROM diezmos  WHERE 

ndom='{$nd}'";
           $resultado=mysqli_query($conexion,$consulta);

            if($reg=mysqli_fetch_array($resultado)){
                $json['datos'][]=$reg;
            }
            mysqli_close($conexion);
            echo json_encode($json);
        }
        else{
            $results['nd']='';
            $results['fd']='';
            $results['db']='';
            $results['sb1']='';
            $results['d1']='';
            $results['sb2']='';
            $results['d2']='';
            $results['sb3']='';
            $results['d3']='';
            $results['d4']='';
            $results['sb4']='';         
            $results['d5']='';
            $results['sb5']='';
            $results['d6']='';
            $results['td']='';
            $results['dn']='';
            $json['datos'][]=$results;
            echo json_encode($json);
        }
    }
    else{   
            $results['nd']='';
            $results['fd']='';
            $results['db']='';
            $results['sb1']='';
            $results['d1']='';
            $results['sb2']='';
            $results['d2']='';
            $results['sb3']='';
            $results['d3']='';
            $results['d4']='';
            $results['sb4']='';         
            $results['d5']='';
            $results['sb5']='';
            $results['d6']='';
            $results['td']='';
            $results['dn']='';
            $json['datos'][]=$results;
            echo json_encode($json);
        }
?>

当您按下“注册十分之一”按钮时,其作用是将在每个字段中输入的数据发送到位于phpmyadmin的bd中,但是,这不再起作用,它不会记录数据,但是会发送我出现以下错误:

  

com.android.volley.parseerror:org.json.jsonexception:org.json.JSONArray类型的value []无法转换为JSONObject

传递代码:Log.i ("URL", "Request URL" + url );向我发送此错误

  

我/网址:请求网址https://ipucsegundaalgarrobo.000webhostapp.com/diezmo15.php?nd=5&fd=2019-05-13&db=500000&sb1=500000&d1=105000.0&sb2=395000.0&d2=19750.0&sb3=375250.0&d3=0.0&d4=0.0&sb4=0.0&d5=7505.0&sb5=367745.0&d6=3677.449951171875&td=135932.453125&dn=364067.5625

这是logcat:

  

d / Volley:[142] BasicNetwork.logSlowRequests:请求的HTTP响应= <[]

在此先感谢您提供的任何信息。 enter image description here

1 个答案:

答案 0 :(得分:0)

您的服务器正在返回JsonArray,即:$json是一个数组,而不是JsonObject,因为您已经将JsonObjectRequest更改为JsonArrayRequestStringRequest