在android中调用php函数在mysql中添加空白记录

时间:2018-01-16 05:09:34

标签: php android mysql

我创建了用于将数据添加到本地主机服务器的Android应用程序。问题是每次我输入数据并提交时,Android应用程序中都没有报告错误。但是添加到mysql数据库的数据在所有字段中都是空白的。有谁知道如何克服这个问题?

正如一位编码员已经问过我已经添加了代码

package localhost80.sample1; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.widget.Toast; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLEncoder; 
public class serverLinking extends AsyncTask<String, Void, String> 
{ 
   private String link; 
   private Context context; 
   private String ed,lno,par,pcs,mts,wt,ct,var,gw,fw,remarks,method; 

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

   protected void onPreExecute() 
    { 
        super.onPreExecute(); 
    } 
@Override 
   protected String doInBackground(String... arg0) 
    { 
        method = arg0[11]; 
        link = "http://192.168.0.104/narmadaa_trial1/android/add.php";         
        if(method.equals("POST")) 
        { 
            ed = arg0[0]; 
            lno = arg0[1]; 
            par = arg0[2]; 
            pcs = arg0[3]; 
            mts = arg0[4]; 
            wt = arg0[5]; 
            ct = arg0[6]; 
            var = arg0[7]; 
            gw = arg0[8]; 
            fw = arg0[9]; 
            remarks = arg0[10]; 

            try 
            { 
                URL url = new URL(link); 
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod(method);    
                httpURLConnection.setDoOutput(true); 
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8")); 
                String data = URLEncoder.encode("ed","UTF-8")+"="+URLEncoder.encode("lno","UTF-8")+"="+URLEncoder.encode("par","UTF-8")+"="+ URLEncoder.encode("pcs","UTF-8")+"="+URLEncoder.encode("mts","UTF-8")+"="+URLEncoder.encode("wt","UTF-8")+"="+ URLEncoder.encode("ct","UTF-8")+"="+URLEncoder.encode("var","UTF-8")+"="+URLEncoder.encode("gw","UTF-8")+"="+ URLEncoder.encode("fw","UTF-8")+"="+URLEncoder.encode("remarks","UTF-8"); 
                bufferedWriter.write(data);
                bufferedWriter.flush(); 
                bufferedWriter.close(); 
                os.close(); 
                InputStream IS = httpURLConnection.getInputStream();
                IS.close(); 
                return "Data Added Successfully"; 
        } 
    catch (MalformedURLException e)
    { 
        e.printStackTrace();
     } 
    catch (IOException e)
    { 
        e.printStackTrace(); 
    } 
   } 
  return null; 
} 

    protected void onPostExecute(String result) 
    { 
        Toast.makeText(context,result,Toast.LENGTH_LONG).show(); 
    } 

    protected void onProgressUpdate(Void... values) 
    { 
        super.onProgressUpdate(values); 
    } 
}

2 个答案:

答案 0 :(得分:0)

非常感谢@Meow Kim提出的宝贵建议。我已经四舍五入并找到了问题的结论,这只是发布请求中的遗失标志造成了可怕的问题。 还要感谢@Magnus Eriksson不断提出的建议来帮助我。所以上面提到的最终代码如下。

AddEntry.java(刚刚重命名为Sample1.java主要活动文件以便于识别)

package am.dx.narmadaa.addentry;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class addEntry extends AppCompatActivity {

public EditText Eed, Elno, Epar, Epcs, Emts, Ewt, Ect, Egw, Efw, Erem;
public Spinner Evar;
public String str_ed, str_lno, str_par, str_pcs, str_mts, str_wt, str_ct, str_var, str_gw, str_fw, str_rem, str_var_code;
public Button Add, Clr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_entry);
    addInput();
}

public void addInput() {
    Eed = findViewById(R.id.ed);
    Elno = findViewById(R.id.lno);
    Epar = findViewById(R.id.par);
    Epcs = findViewById(R.id.pcs);
    Emts = findViewById(R.id.mts);
    Ewt = findViewById(R.id.wt);
    Ect = findViewById(R.id.ct);
    Egw = findViewById(R.id.gw);
    Efw = findViewById(R.id.fw);
    Erem = findViewById(R.id.rem);
    Evar = findViewById(R.id.var);
    Add = findViewById(R.id.btnAdd);
    Clr = findViewById(R.id.btnClr);

    Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            str_ed = (Eed.getText()).toString();
            str_lno = (Elno.getText()).toString();
            str_par = (Epar.getText()).toString();
            str_pcs = (Epcs.getText()).toString();
            str_mts = (Emts.getText()).toString();
            str_wt = (Ewt.getText()).toString();
            str_ct = (Ect.getText()).toString();
            str_gw = (Egw.getText()).toString();
            str_fw = (Efw.getText()).toString();
            str_rem = (Erem.getText()).toString();
            str_var = (Evar.getSelectedItem()).toString();

            switch (Evar.getSelectedItemPosition()) {
                case 0:
                    str_var_code = "fw";
                    break;
                case 1:
                    str_var_code = "brw";
                    break;
                case 2:
                    str_var_code = "mw";
                    break;
                case 3:
                    str_var_code = "bw";
                    break;
                case 4:
                    str_var_code = "ow";
                    break;
            }
            displayToast(str_ed, str_lno, str_par, str_pcs, str_mts, str_wt, str_ct, str_var, str_gw, str_fw, str_rem);
            serverLinking hostlink = new serverLinking(addEntry.this);
            hostlink.execute(str_ed, str_lno, str_par, str_pcs, str_mts, str_wt, str_ct, str_var_code, str_gw, str_fw, str_rem, "POST");
        }
    });

    Clr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Eed.setText("");
            Elno.setText("");
            Epar.setText("");
            Epcs.setText("");
            Emts.setText("");
            Ewt.setText("");
            Ect.setText("");
            Egw.setText("");
            Efw.setText("");
            Erem.setText("");
            Evar.setSelection(0);
        }
    });
}

private void displayToast(String d_ed, String d_lno, String d_par, String d_pcs, String d_mts, String d_wt, String d_ct, String d_var, String d_gw, String d_fw, String d_rem) {
    Toast toast = new Toast(getApplicationContext());
    String toasttext;
    toasttext = "Entry date : " + d_ed + "\nLot no : " + d_lno + "\nParty : " + d_par + "\nTotal Pcs : " + d_pcs + "\nTotal mtrs : " + d_mts + "\nTotal Weight : " + d_wt + "\nCount : " + d_ct + "\nVariety : " + d_var + "\nGray Width : " + d_gw + "\nFinishing Width : " + d_fw + "\nRemarks : " + d_rem;
    toast.setGravity(Gravity.TOP | Gravity.START, 0, 0);
    Toast.makeText(addEntry.this, toasttext, Toast.LENGTH_SHORT).show();
}
}

ServerLinking.java(服务器的连接器文件)

package am.dx.narmadaa.addentry;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class serverLinking extends AsyncTask<String, Void, String> 
{
private String link;
private Context context;
private String error;
private String ed, lno, par, pcs, mts, wt, ct, var, gw, fw, remarks, method;

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

protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(String... arg0) {
    method = arg0[11];
    link = "http://192.168.0.101/android/add.php";

    if (method.equals("POST")) {
        ed = arg0[0];
        lno = arg0[1];
        par = arg0[2];
        pcs = arg0[3];
        mts = arg0[4];
        wt = arg0[5];
        ct = arg0[6];
        var = arg0[7];
        gw = arg0[8];
        fw = arg0[9];
        remarks = arg0[10];
        try {
            URL url = new URL(link);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod(method);
            httpURLConnection.setDoOutput(true);
            OutputStream os = httpURLConnection.getOutputStream();

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

            String data = URLEncoder.encode("ed", "UTF-8") + "=" + URLEncoder.encode(ed, "UTF-8") + "&" +
                    URLEncoder.encode("lno", "UTF-8") + "=" + URLEncoder.encode(lno, "UTF-8") + "&" +
                    URLEncoder.encode("par", "UTF-8") + "=" + URLEncoder.encode(par, "UTF-8") + "&" +
                    URLEncoder.encode("pcs", "UTF-8") + "=" + URLEncoder.encode(pcs, "UTF-8") + "&" +
                    URLEncoder.encode("mts", "UTF-8") + "=" + URLEncoder.encode(mts, "UTF-8") + "&" +
                    URLEncoder.encode("wt", "UTF-8") + "=" + URLEncoder.encode(wt, "UTF-8") + "&" +
                    URLEncoder.encode("ct", "UTF-8") + "=" + URLEncoder.encode(ct, "UTF-8") + "&" +
                    URLEncoder.encode("var", "UTF-8") + "=" + URLEncoder.encode(var, "UTF-8") + "&" +
                    URLEncoder.encode("gw", "UTF-8") + "=" + URLEncoder.encode(gw, "UTF-8") + "&" +
                    URLEncoder.encode("fw", "UTF-8") + "=" + URLEncoder.encode(fw, "UTF-8") + "&" +
                    URLEncoder.encode("remarks", "UTF-8") + "=" + URLEncoder.encode(remarks, "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            os.close();

            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            return "Data Added Successfully";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            error = e.toString();
        } catch (IOException e) {
            e.printStackTrace();
            error = e.toString();
        }
    }
    return "Data Addition Failed\n Error Details : " + error;
}

//    @Override
protected void onPostExecute(String result) {
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}

protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
}

Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="am.dx.narmadaa.addentry">

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".addEntry">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

机器人/ add.php

php server file
<?php
     include '../database.php';

     $ed = filter_input(INPUT_POST, "ed",FILTER_SANITIZE_STRING);

     if(strlen($ed)==8)
     {
     $d = substr($ed, 0, 2);
     $m = substr($ed,2,2);
     $y = substr($ed, 4, 4);
}
if(strlen($ed)==6)
{
$d = substr($ed, 0, 2);
$m = substr($ed,2,2);
$y = "20".substr($ed, 4, 2);
}
if(strlen($ed)==4)
{
    $d = substr($ed, 0, 2);
    $m = substr($ed, 2, 2);
    $y = date("Y");
}
 else 
{
$d = "00";
$m = "00";
$y = "0000";
}

$newed = implode("-", array($y,$m,$d));
$lno = filter_input(INPUT_POST, "lno",FILTER_SANITIZE_STRING);
$par = filter_input(INPUT_POST, "par",FILTER_SANITIZE_STRING);
$pcs = filter_input(INPUT_POST, "pcs",FILTER_SANITIZE_NUMBER_INT);
$mts = filter_input(INPUT_POST, "mts",FILTER_SANITIZE_NUMBER_FLOAT);
$wt = filter_input(INPUT_POST, "wt",FILTER_SANITIZE_NUMBER_FLOAT);
$ct = filter_input(INPUT_POST, "ct",FILTER_SANITIZE_STRING);
$var = filter_input(INPUT_POST, "var",FILTER_SANITIZE_STRING);
$gw = filter_input(INPUT_POST, "gw",FILTER_SANITIZE_NUMBER_FLOAT);
$fw = filter_input(INPUT_POST, "fw",FILTER_SANITIZE_NUMBER_FLOAT);
$remarks = filter_input(INPUT_POST, "remarks",FILTER_SANITIZE_STRING);

echo $newed."<br/>".$lno."<br/>".$par."<br/>".$pcs."<br/>".$mts."<br/>".$wt."<br/>".$ct."<br/>".$var."<br/>".$gw."<br/>".$fw."<br/>".$remarks;

$sqlInsertAndroid = "INSERT INTO lems(ed,lno,par,pcs,mts,wt,ct,var,gw,fw,remarks,sluo) VALUES('".$newed."','".$lno."','".$par."','".$pcs."','".$mts."','".$wt."','".$ct."','".$var."','".$gw."','".$fw."','".$remarks."',now())";
echo "<br/>".$sqlInsertAndroid;
$dataInsertAndroid = mysqli_query($dbconn, $sqlInsertAndroid);
?>

答案 1 :(得分:-1)

package localhost80.sample1;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
public class serverLinking extends AsyncTask < String, Void, String > {
    private String link;
    private Context context;
    private String ed, lno, par, pcs, mts, wt, ct,
    var, gw, fw, remarks, method;
    public serverLinking(Context context) {
        this.context = context;
    }
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override protected String doInBackground(String...arg0) {
        method = arg0[11];
        link = "http://192.168.0.104/narmadaa_trial1/android/add.php";
        if (method.equals("POST")) {
            ed = arg0[0];
            lno = arg0[1];
            par = arg0[2];
            pcs = arg0[3];
            mts = arg0[4];
            wt = arg0[5];
            ct = arg0[6];
            var = arg0[7];
            gw = arg0[8];
            fw = arg0[9];
            remarks = arg0[10];
            try {
                URL url = new URL(link);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod(method);
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode("ed", "UTF-8") + "=" + URLEncoder.encode("lno", "UTF-8") + "=" + URLEncoder.encode("par", "UTF-8") + "=" + URLEncoder.encode("pcs", "UTF-8") + "=" + URLEncoder.encode("mts", "UTF-8") + "=" + URLEncoder.encode("wt", "UTF-8") + "=" + URLEncoder.encode("ct", "UTF-8") + "=" + URLEncoder.encode("var", "UTF-8") + "=" + URLEncoder.encode("gw", "UTF-8") + "=" + URLEncoder.encode("fw", "UTF-8") + "=" + URLEncoder.encode("remarks", "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Data Added Successfully";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    protected void onPostExecute(String result) {
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
    protected void onProgressUpdate(Void...values) {
        super.onProgressUpdate(values);
    }
}

检查你的查询字符串(字符串数据)。 传递参数需要该名称。 和分隔符是&amp;

value1 = value2 = value3 ....(x)

value1name = value1&amp; value2name = value2&amp; value3name = value3 ...(o)

示例

String data = "ed=" + ed
            + "&lno=" + lno
            + "&par=" + par
            + ....

检查add.php上收到的数据($ _ GET或$ _REQUEST)