PHP响应HTTP请求的Andr​​oid [英] PHP Response to HTTP Request From Android

查看:227
本文介绍了PHP响应HTTP请求的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多找到一种方法来发送HTTP响应到Android应用程序发送HTTP请求用户名和密码

我的问题是我想借此从Android应用程序的用户名和密码,并从3列(toggle1,toggle2,toggle3)数据库发送回值用户

我现在已经看到了所有的例子只发送1或0只检查用户名和密码,如果它的正确与否,但我还需要从数据库中发送列的数据,我preFER这不是JSON

发送HTTP请求和另外活动中读取数据

 进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口java.net.HttpURLConnection中;
进口的java.net.URL;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类HttpLogin延伸活动{
    / **当第一次创建活动调用。 * /
    私人按钮登录;
    私人的EditText用户名,密码;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        登录=(按钮)findViewById(R.id.login);
        用户名=(的EditText)findViewById(R.id.username);
        密码=(EditText上)findViewById(R.id.password);        login.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                。字符串mUsername = username.getText()的toString();
                。字符串mPassword = password.getText()的toString();                tryLogin(mUsername,mPassword);
            }
        });
    }    保护无效tryLogin(字符串mUsername,字符串mPassword)
    {
        HttpURLConnection的连接;
       OutputStreamWriter请求= NULL;            网址URL = NULL;
            串响应=无效;
            字符串参数=用户名=+ mUsername +&放大器;密码=+ mPassword;            尝试
            {
                URL =新的URL(您的登录URL);
                连接=(HttpURLConnection类)url.openConnection();
                connection.setDoOutput(真);
                connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
                connection.setRequestMethod(POST);                请求=新OutputStreamWriter(connection.getOutputStream());
                request.write(参数);
                request.flush();
                request.close();
                串线=;
                InputStreamReader的ISR =新的InputStreamReader(connection.getInputStream());
                读者的BufferedReader =新的BufferedReader(ISR);
                StringBuilder的SB =新的StringBuilder();
                而((行= reader.readLine())!= NULL)
                {
                    sb.append(行+\\ n);
                }
                //登录过程后从服务器响应将被存储在响应变量。
                响应= sb.toString();
                //你可以在这里进行UI操作
                Toast.makeText(这一点,从服务器消息:\\ n+响应,0).show();
                isr.close();
                reader.close();            }
            赶上(IOException异常E)
            {
                //错误
            }
    }
}

接收用户名和密码,我需要这个文件同时也发送数据库列PHP文件中的用户名和密码

 < PHP
$主机=localhost的; //主机名
$ USER =测试; // MySQL用户名
$ PSWD =123; // mysql的密码
$ DB =pet_home; //数据库名称
// $ tbl_name表=用户; //表名$康恩= mysql_connect($主机,$用户,$ PSWD);
mysql_select_db($数据库,$康恩);
$用户名= $ _ POST [用户名];
$密码= $ _ POST ['密码'];
$结果= mysql_query(从用户选择*其中username ='$用户名和密码= $密码')或死亡(mysql_error());
$数= mysql_num_rows($结果);
$行= mysql_fetch_array($结果);
        如果($计数大于0){
        回音1;
        }其他{
        呼应0;
        }
?>


解决方案

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

它看起来像目前的反应仅仅是回显的值0或1

为什么不直接把它们添加到字符串由像管道或星号的罕见字符分隔然后分裂回来了?

您回声那么会是这样的。

  1,呼应|$行[0]|$行[1]。;

等。

**另外,我要补充的obigitory散列密码,并考虑安全

I've searched alot to find a way to send HTTP Response to Android Application that sends HTTP Request with Username and Password

My problem is I want to take the username and password from android application and send back the values for that user from 3 columns (toggle1,toggle2,toggle3) in the database

All examples I've seen now only send 1 or 0 just for checking username and password if it's correct or not but I need to send also Columns Data from Database, I Prefer it's not JSON

Activity that sends HTTP Request and Also reads data

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 File That Receives username and password and I need this file to also send database Columns values for the username and password

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

解决方案

The values you want will be in the $row array. You can access them (depending on the column you want) like this $row[0], $row[1], etc.

It looks like the response currently is just the echo'd value 0 or 1

Why not just append them to that string separated by an uncommon character like the pipe or asterisk then split it back up?

Your echo then would be something like

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

etc.

** Also, I should add the obigitory "hash your passwords and consider security"

这篇关于PHP响应HTTP请求的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆