2014-07-14 83 views
0

我發送來自Android客戶端用戶名的PHP服務器,但它似乎POST不工作,用戶名不是由服務器讀取...httpPost Android的PHP

的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代碼

<?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); 
} 
?> 

的錯誤是$日誌= $ _ POST [ '登錄']未定義

+0

嘗試$ _REQUEST而不是後 – KOTIOS

+0

它doen't工作,同樣的結果 –

+0

是烏拉圭回合的遠程或本地的API? – KOTIOS

回答

0

Ť RY這個代碼 你只是在你的代碼留下一個單行休息,一切都很好

<?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); 
    } 
    ?>