【问题标题】:PHP Response to HTTP Request From AndroidPHP 对来自 Android 的 HTTP 请求的响应
【发布时间】:2013-12-13 20:10:01
【问题描述】:

我已经搜索了很多以找到一种向 Android 应用程序发送 HTTP 响应的方法,该应用程序使用用户名和密码发送 HTTP 请求

我的问题是我想从 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;
        }
?>

【问题讨论】:

    标签: php android mysql post response


    【解决方案1】:

    您想要的值将在 $row 数组中。您可以像这样访问它们(取决于您想要的列)$row[0]、$row[1] 等。

    看起来当前的响应只是回显的值 0 或 1

    为什么不将它们附加到由不常见字符(如管道或星号)分隔的字符串中,然后将其拆分回来?

    你的回声会是这样的

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

    等等

    ** 另外,我应该添加“哈希密码并考虑安全性”

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 2023-03-04
        • 2018-06-07
        • 1970-01-01
        • 2012-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多