2013-04-04 25 views
1

我正在爲登錄過程創建一個簡單的測試應用程序,但是我得到的錯誤是當我在模擬器中使用瀏覽器並嘗試使用網址時給我的網址網頁不可用 我用本地主機127.0.0.2,但它給了我相同的味精,我用我的電腦的IP相同的味精任何人都可以幫助我?android + php + mysql

Java代碼

package com.sencide; 

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

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.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 

import android.os.Bundle; 
import android.os.StrictMode; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AndroidLogin extends Activity implements OnClickListener { 


    Button ok,back,exit; 
    TextView result; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

    // Login button clicked 
     ok = (Button)findViewById(R.id.btn_login); 
     ok.setOnClickListener(this); 

     result = (TextView)findViewById(R.id.lbl_result); 



    } 



    public void postLoginData() { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     Log.e("Responce-->","after httpclient"); 
     /* login.php returns true if username and password is equal to saranga */ 
     HttpPost httppost = new HttpPost("http://192.168.1.70/login.php"); 
     Log.e("Responce-->","after httppost"); 
     try { 
      // Add user name and password 
     EditText uname = (EditText)findViewById(R.id.txt_username); 
     String username = uname.getText().toString(); 

     EditText pword = (EditText)findViewById(R.id.txt_password); 
     String password = pword.getText().toString(); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("username", username)); 
      nameValuePairs.add(new BasicNameValuePair("password", password)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      Log.e("Responce-->","after using the list name pair"); 

      // Execute HTTP Post Request 
      Log.w("SENCIDE", "Execute HTTP Post Request"); 
      HttpResponse response = httpclient.execute(httppost); 
      Log.e("Responce-->","after execute the http response"); 
      // String str = inputStreamToString(response.getEntity().getContent()).toString(); 
      String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8); 
      //Log.w("SENCIDE", str); 

      Log.e("Responce-->",""+str); 

      if(str.toString().equalsIgnoreCase("true")) 
      { 
      Log.w("SENCIDE", "TRUE"); 
      result.setText("Login successful"); 
      }else 
      { 
      Log.w("SENCIDE", "FALSE"); 
      result.setText(str);    
      } 

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



    private StringBuilder inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuilder total = new StringBuilder(); 
     // Wrap a BufferedReader around the InputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     // Read response until the end 
     try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
     // Return full string 
     return total; 
     } 

     /* login.php returns true if username and password is equal to saranga */ 


    @Override 
    public void onClick(View view) { 
     // TODO Auto-generated method stub 
     if(view == ok){ 
      Thread t = new Thread(){ 
        public void run(){ 
         postLoginData(); 
        } 
       }; 
       t.start(); 

      } 
    } 





} 

PHP代碼

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="testlogin"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 
echo "true"; 
} 
else { 
echo "Login Failed"; 
} 
?> 
+3

本地主機爲127.0.0.1 – Sammitch 2013-04-04 17:51:31

+0

什麼,當你瀏覽你的桌面上的登錄頁面您使用的地址?在模擬器上也使用它。 還有一些信息丟失(而其他的,如java代碼,是過度殺毒):模擬器是否有正確的連接?你是否在桌面上使用了防火牆? – Rick77 2013-04-04 17:54:08

+0

您是否需要使用192.168.0.x(在windows上檢查ifconfig或ipconfig以獲取真實機器IP地址)? – 2013-04-04 17:55:39

回答

-1

嘗試擊中使用捲曲(如果你使用的是UNIX操作系統),你的PHP服務器的請求,PHP或做一個簡單的網頁使用。確保它給你一個有效的(200)響應。

<html> 
    <head> 
    </head> 
    <body> 
    <form action="http://localhost/{{ your-php-script }}" method="POST"> 
     <input type="text" name="username"> 
     <input type="text" name="password"> 
     <input type="submit" value="submit"> 
    </form> 
    </body> 
</html> 

此外,host pc should be 10.0.2.2,不是127.0.0.2(或127.0.0.1)

+0

它沒有工作,我改變了網址爲:** 10.0.2.2,而不是127.0。 0.2(或127.0.0.1)** – user2214618 2013-04-04 18:35:10

+0

你用什麼網絡服務器託管PHP?它託管在您用來運行模擬器的相同框中,對吧? – pnovotnak 2013-04-04 18:46:53

+0

它的工作我可以通過輸入10.0.2.2得到頁面我以前試過,但它沒有工作,但現在我得到一個錯誤味精在日誌貓你能幫助我?? [http://stackoverflow.com/questions/15819267 /機器人-PHP-登錄進程MySQL的] – user2214618 2013-04-04 18:55:49