我正在尝试将数据从Android应用程序发送到localhost,但数据不在localhost上提交

时间:2016-07-15 07:57:42

标签: java php android

我正在尝试将数据从我的Android应用程序发送到localhost但是数据不在localhost上提交。我已经创建了php文件(register.php)并保存在www目录中。当我手动提交数据时,php文件工作正常。 下面是我的MainActivity.java文件。

public void onButtonClickListener()
    {
        on_submit=(Button)findViewById(R.id.onsubmit);
        on_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               String str_personname = personname.getText().toString();
                String str_contactno = contactno.getText().toString();
                String str_alternateno = alternateno.getText().toString();
                String str_emailid = emailid.getText().toString();
                String str_currentline1 = currentline1.getText().toString();
                String str_currentline2 = currentline2.getText().toString();
                String str_currentlocality = currentlocality.getText().toString();
                String str_currentcity = currentcity.getText().toString();
                String str_currentstate = currentstate.getText().toString();
                String str_currentpin = currentpin.getText().toString();
                String str_permanentline1 = permanentline1.getText().toString();
                String str_permanentline2 = permanentline2.getText().toString();
                String str_permanentlocality = permanentlocality.getText().toString();
                String str_permanentcity = permanentcity.getText().toString();
                String str_permanentstate = permanentstate.getText().toString();
                String str_permanentpin = permanentpin.getText().toString();
                String str_dateofinterview = dateofinterview.getText().toString();
                String str_applyfor = applyfor.getText().toString();
                String str_refrencename = refrencename.getText().toString();
                String str_refrenceid = refrenceid.getText().toString();
                 String type = "register";
                Backendworker backendworker = new Backendworker(MainActivity.this);
                backendworker.execute(type, str_personname, str_contactno, str_alternateno, str_emailid, str_currentline1, str_currentline2, str_currentlocality, str_currentcity,
                        str_currentstate, str_currentpin, str_permanentline1, str_permanentline2, str_permanentlocality, str_permanentcity, str_permanentstate,
                        str_permanentpin, str_dateofinterview, str_applyfor, str_refrencename, str_refrenceid);
                Toast.makeText(MainActivity.this, "Action performed on button click", Toast.LENGTH_SHORT).show();

            }
        });
    }

我已经在oncreate()方法中定义了他们的id。 下面是我的Backendworker.java类

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 Backendworker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    Backendworker(Context ctx) {

        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String register_url = "http://my ip address or local host/register.php";

       if(type.equals("register")) {
            try {
                String personname1 = params[1];
                String contactno1 = params[2];
                String alternateno1 = params[3];
                String emailid1  = params[4];
                String currentline11 = params[5];
                String currentline21 = params[6];
                String currentlocality1 = params[7];
                String currentcity1 = params[8];
                String currentstate1 = params[9];
                String currentpin1 = params[10];
                String permanentline11 = params[11];
                String permanentline21 = params[12];
                String permanentlocality1 = params[13];
                String permanentcity1 = params[14];
                String permanentstate1= params[15];
                String permanentpin1 = params[16];
                String dateofinterview1 = params[17];
                String applyfor1 = params[18];
                String refencename1 = params[19];
                String refrenceid1 = params[20];




                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("personname", "UTF-8")+"="+URLEncoder.encode(personname1, "UTF-8")
                        +"&"+URLEncoder.encode("contactno", "UTF-8")+"="+URLEncoder.encode(contactno1, "UTF-8")
                        +"&"+URLEncoder.encode("alternateno", "UTF-8")+"="+URLEncoder.encode(alternateno1, "UTF-8")
                        +"&"+URLEncoder.encode("emailid", "UTF-8")+"="+URLEncoder.encode(emailid1, "UTF-8")
                        +"&"+URLEncoder.encode("currentline1", "UTF-8")+"="+URLEncoder.encode(currentline11, "UTF-8")
                        +"&"+URLEncoder.encode("currentline2", "UTF-8")+"="+URLEncoder.encode(currentline21, "UTF-8")
                        +"&"+URLEncoder.encode("currentlocality", "UTF-8")+"="+URLEncoder.encode(currentlocality1, "UTF-8")
                        +"&"+URLEncoder.encode("currentcity", "UTF-8")+"="+URLEncoder.encode(currentcity1, "UTF-8")
                        +"&"+URLEncoder.encode("currentstate", "UTF-8")+"="+URLEncoder.encode(currentstate1, "UTF-8")
                        +"&"+URLEncoder.encode("currentpin","UTF-8")+"="+URLEncoder.encode(currentpin1,"UTF-8")
                        +"&"+URLEncoder.encode("permanentline1","UTF-8")+"="+URLEncoder.encode(permanentline11,"UTF-8")
                        +"&"+URLEncoder.encode("permanentline2","UTF-8")+"="+URLEncoder.encode(permanentline21,"UTF-8")
                        +"&"+URLEncoder.encode("permanentlocality","UTF-8")+"="+URLEncoder.encode(permanentlocality1,"UTF-8")
                        +"&"+URLEncoder.encode("permanentcity","UTF-8")+"="+URLEncoder.encode(permanentcity1,"UTF-8")
                        +"&"+URLEncoder.encode("permanentstate","UTF-8")+"="+URLEncoder.encode(permanentstate1,"UTF-8")
                        +"&"+URLEncoder.encode("permanentpin","UTF-8")+"="+URLEncoder.encode(permanentpin1,"UTF-8")
                        +"&"+URLEncoder.encode("dateofinterview","UTF-8")+"="+URLEncoder.encode(dateofinterview1,"UTF-8")
                        +"&"+URLEncoder.encode("applyfor","UTF-8")+"="+URLEncoder.encode(applyfor1,"UTF-8")
                        +"&"+URLEncoder.encode("refrencename","UTF-8")+"="+URLEncoder.encode(refencename1,"UTF-8")
                        +"&"+URLEncoder.encode("refrenceid","UTF-8")+"="+URLEncoder.encode(refrenceid1,"UTF-8");
                        //+ "&" +URLEncoder.encode("password","UTF-8")+"="+ URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

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

代码在任何地方都不会显示任何错误。 下面是php文件

<?php
require "conn.php";
$personname=$_POST["personname"];
$contactno=$_POST["contactno"];
$alternateno=$_POST["alternateno"];
$emailid=$_POST["emailid"];
$currentline1=$_POST["currentline1"];
$currentline2=$_POST["currentline2"];
$currentlocality=$_POST["currentlocality"];
$currentcity=$_POST["currentcity"];
$currentstate=$_POST["currentstate"];
$currentpin=$_POST["currentpin"];
$permanentline1=$_POST["permanentline1"];
$permanentline2=$_POST["permanentline2"];
$permanentlocality=$_POST["permanentlocality"];
$permanentcity=$_POST["permanentcity"];
$permanentstate=$_POST["permanentstate"];
$permanentpin=$_POST["permanentpine"];
$dateofinterview=$_POST["dateofinterview"];
$applyfor=$_POST["applyfor"];
$refrencename=$_POST["refrencename"];
$refrenceid=$_POST["refrenceid"];
$mysql_qry="insert into applicant(personname,contactno,alternateno,emailid,currentline1,currentline2,currentlocality,currentcity,currentstate,currentpin,permanentline1,permanentline2,permanentlocality,permanentcity,permanentstate,permanentpin,dateofinterview,applyfor,refrencename,refrenceid) values('$personname','$contactno','$alternateno','$emailid','$currentline1','$currentline2','$currentlocality','$currentcity','$currentstate','$currentpin','$permanentline1','$permanentline2','$permanentlocality','$permanentcity','$permanentstate','$permanentpin','$dateofinterview','$applyfor','$refrencename','$refrenceid')";

if($conn->query($mysql_qry)===TRUE)
{
echo "insert successful";
}
else
{
echo "insert unsuccessful";
}
$conn->close();
?>

如果有人知道ans那么请帮忙。 感谢...

0 个答案:

没有答案