PHP脚本没有从ANDROID代码中获取值

时间:2016-06-19 23:37:58

标签: php android mysql post

我无法捕捉从android代码到PHP脚本的值##这是我的backgroundworker.java文件##每当我点击我的应用程序上的注册按钮它只在ID字段中的数据库中放置记录其他仍为空

    package com.example.danish.moviestop;

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.URL;
import java.net.URLEncoder;
import java.nio.charset.MalformedInputException;


/**
 * Created by Danish on 6/18/2016.
 */
public class BackgroundWorker extends AsyncTask<String,Void,String> {


    Context context;
    AlertDialog alertDialog;

    BackgroundWorker(Context ctx) {
        context = ctx;
    }




    @Override

    protected String doInBackground(String... params) {

        String type = params[0];
        String login_url = "http://192.168.1.2/app/login.php";
        String register_url = "http://192.168.1.2/app/register.php";

        if (type.equals("login")) {
            try {


                String user_name = params[1];
                String password = params[2];

                URL url = new URL(login_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("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "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 (MalformedInputException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();

            }


        } else

        {




            try{


                String name=params[1];
                String surname=params[2];
                String age=params[3];
                String username=params[4];
                String password=params[5];


                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 (  "name"     , "UTF-8")  +  "="  +  URLEncoder.encode ( name      ,  "UTF-8")  +  "&"
                                  + URLEncoder.encode (  "surname"  , "UTF-8")  +  "="  +  URLEncoder.encode ( surname   ,  "UTF-8")  +  "&"
                                  + URLEncoder.encode (  "age"      , "UTF-8")  +  "="  +  URLEncoder.encode ( age       ,  "UTF-8")  +  "&"
                                  + URLEncoder.encode (  "username" , "UTF-8")  +  "="  +  URLEncoder.encode ( username  ,  "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 (MalformedInputException 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 code

    <?php

require "conn.php";

$name=$POST["name"];
$surname=$POST["surname"];
$age=$POST["age"];
$username=$POST["username"];
$password=$POST["password"];


$mysql_qry=" insert into logins(name,surname,age,username,password) values ('$name','$surname','$age','$username','$password')";


if($conn->query($mysql_qry)==TRUE){

    echo "insert successful";


}
else {


    echo "Error: ". $mysql_qry . "<br>" . $conn->error;
}

$conn->close();


?>
 and it shows me the error in below image.... 
[error coming from the response of php script][1]


  [1]: http://i.stack.imgur.com/SDIGp.gif

any help ?

1 个答案:

答案 0 :(得分:1)

尝试使用$ _POST代替$ POST。

http://php.net/manual/en/reserved.variables.post.php