httpPost android php

时间:2014-07-14 12:28:47

标签: php android

我正在从android客户端向php服务器发送用户名,但似乎POST不起作用且服务器不读取用户名...

android代码

package com.example.election;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class authentification extends Activity {

    Button Quitter, Annuler, Valider;
    EditText login,password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authentification);
        Quitter = (Button) findViewById(R.id.Quitter);
        Valider = (Button) findViewById(R.id.Valider);
        Annuler = (Button) findViewById(R.id.Annuler);
        login = (EditText) findViewById(R.id.login);

        password = (EditText) findViewById(R.id.password);


        Valider.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                verifier_admin(v);

            }
        });


        Quitter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);

            }
        });

    }


    public void reset(View v){
        login = (EditText) findViewById(R.id.login);
        password = (EditText) findViewById(R.id.password);
        login.setText("");
        password.setText("");

    }

    public void verifier_admin(View v){
        InputStream is = null;
        HttpClient httpclient = null;
        HttpPost httppost = null;
        HttpResponse response = null;
        HttpEntity entity = null;

        ArrayList<NameValuePair> nameValuePairs = null; 
        String pass;
        String log = login.getText().toString();

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://172.16.50.239:80/android/test.php");
        HttpContext localContext = new BasicHttpContext();

        pass =password.getText().toString();

        if(log.length()>0 ){

            try{
                List<NameValuePair> var = new ArrayList<NameValuePair>(2);  

                var.add(new BasicNameValuePair("log",log));

                //var.add(new  BasicNameValuePair("pass",pass));
               // post.setEntity(new UrlEncodedFormEntity (var,"utf-8"));
                post.setEntity(new UrlEncodedFormEntity (var));
                ResponseHandler<String> responseHandler=new BasicResponseHandler();
                String reponse =client.execute(post,responseHandler);

                //BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                //String s = reader.readLine();
                Toast.makeText(this, "message envoyé",Toast.LENGTH_SHORT).show();
                login.setText("");


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

        }

        else {
            Toast.makeText(this, "veuillez entrez vos données",Toast.LENGTH_SHORT).show();

        }
}
}

php code

<?php
if( isset($_POST)  ){
print_r($_POST);




//connexion base
$link = mysql_connect("localhost","root","") or die ('ERREUR '.mysql_error()) ;
$bd_selected=mysql_select_db("mydb",$link) or die ('ERREUR1 '.mysql_error());

$req1=" SELECT * FROM user WHERE (login= '$log')";

$sql=mysql_query($req); 
$data = mysql_num_rows($sql);// affiche le nombre de ligne satisfaisant condition
echo $req;



mysql_close($link);
}
?>

错误是$ log = $ _ POST ['log'] undefined

1 个答案:

答案 0 :(得分:0)

试用此代码 你只需在代码中留下一行就可以了。

    <?php    
    $log=$_POST['log'];   // Here define log value in $log variable
    if( isset($_POST)  )
    {
    print_r($_POST);

//connexion base

    $link = mysql_connect("localhost","root","") or die ('ERREUR '.mysql_error()) ;
    $bd_selected=mysql_select_db("mydb",$link) or die ('ERREUR1 '.mysql_error());

    $req1=" SELECT * FROM user WHERE (login= '$log')";

    $sql=mysql_query($req1); 
    $data = mysql_num_rows($sql);// affiche le nombre de ligne satisfaisant condition
    echo $req1;    
    mysql_close($link);
    }
    ?>