2013-12-13 190 views
0

我搜索了很多找到一種方法來發送HTTP響應到Android應用程序發送HTTP請求用戶名和密碼PHP響應HTTP請求的Android

我的問題是我想從繁重的用戶名和密碼Android應用程序並從數據庫中的3列(toggle1,toggle2,toggle3)發回該用戶的值

我見過的所有示例現在只發送1或0,僅用於檢查用戶名和密碼,如果它正確或不正確但我還需要發送來自數據庫的列數據,我寧願它不是JSON

發送的活動小號HTTP請求,並讀取接收的用戶名和密碼數據

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
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 HttpLogin extends Activity { 
    /** Called when the activity is first created. */ 
    private Button login; 
    private EditText username, password; 

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

     login = (Button) findViewById(R.id.login); 
     username = (EditText) findViewById(R.id.username); 
     password = (EditText) findViewById(R.id.password); 

     login.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String mUsername = username.getText().toString(); 
       String mPassword = password.getText().toString(); 

       tryLogin(mUsername, mPassword); 
      } 
     }); 
    } 

    protected void tryLogin(String mUsername, String mPassword) 
    {   
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 

      URL url = null; 
      String response = null;   
      String parameters = "username="+mUsername+"&password="+mPassword; 

      try 
      { 
       url = new URL("your login URL"); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestMethod("POST");  

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write(parameters); 
       request.flush(); 
       request.close();    
       String line = "";    
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       // Response from server after login process will be stored in response variable.     
       response = sb.toString(); 
       // You can perform UI operations here 
       Toast.makeText(this,"Message from Server: \n"+ response, 0).show();    
       isr.close(); 
       reader.close(); 

      } 
      catch(IOException e) 
      { 
       // Error 
      } 
    } 
} 

PHP文件,我需要這個文件也發送數據庫列值的用戶名和密碼

<?php 
$host="localhost"; // Host name 
$user="test"; // Mysql username 
$pswd="123"; // Mysql password 
$db="pet_home"; // Database name 
//$tbl_name="users"; // Table name 

$conn = mysql_connect($host, $user, $pswd); 
mysql_select_db($db, $conn); 
$username=$_POST['username']; 
$password=$_POST['password']; 
$result=mysql_query("select * from users where username='$username' and password='$password'")or die (mysql_error()); 
$count=mysql_num_rows($result); 
$row=mysql_fetch_array($result);  
     if ($count > 0){ 
     echo 1;  
     }else{ 
     echo 0; 
     } 
?> 

回答

1

你想將這些值在$ row數組中。您可以像這樣$ row [0],$ row [1]等訪問它們(取決於您想要的列)。

看起來像當前的響應只是echo'd值0或1

爲什麼不把它們追加到由管道或星號等非常見字符分隔的字符串中,然後再將其分開?

你的迴音,然後會像

echo 1."|".$row[0]."|".$row[1]; 

**另外,我要補充的obigitory 「散列您的密碼和安全考慮」

1

試試這個

try{ 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(url); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        String the_string_response = convertResponseToString(response); 
        Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show(); 
       }catch(Exception e){ 
         Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); 
         System.out.println("Error in http connection "+e.toString()); 
       }