我想在android中创建一个应用程序,用户需要登录应用程序才能使用它。登录认证将由PHP Web服务完成。我有一个login.java
CustomeHTTPClient
类和这一个是样本代码我已经从internet.There得到的是在login.java
类名connectphp
的方法连接到web服务,并使用响应于显示吐司消息Android和PHP登录认证
Login.java
package com.boyzcorn.android.fyp;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
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 login extends Activity {
/** Called when the activity is first created. */
EditText eText ;
EditText eText2 ;
Button btnSubmit ;
Button btnSignup ;
public void validation()
{
if(eText.getText().toString().equals("") ||
eText.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT).show();
}
else
{
connectphp();
}
}
public void connectphp()
{
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));
Passing Parameter to the php web service for authentication
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php", postParameters); //Enter Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(res.equals("1"))
{
Toast.makeText(getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this,order_pushing.class);
startActivity(i);
}
else
if(res.equals("0"))
{
Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
eText.setText(e.toString());
}}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
eText = (EditText)findViewById(R.id.uid);
eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
btnSignup = (Button)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
validation(); (This is to check empty fields)
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(login.this,signup.class);
startActivity(i);
}
});
}}
这是我的customhtpclient
类,其中http连接定义。
**CustomHTTPClient.java**
package com.boyzcorn.android.fyp;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
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 login extends Activity {
/** Called when the activity is first created. */
EditText eText ;
EditText eText2 ;
Button btnSubmit ;
Button btnSignup ;
public void validation()
{
if(eText.getText().toString().equals("") ||
eText.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT).show();
}
else
{
connectphp();
}
}
public void connectphp()
{
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php",postParameters); //Enter Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(res.equals("1"))
{
Toast.makeText(getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this,order_pushing.class);
startActivity(i);
}
else
if(res.equals("0"))//Server response if 0
{
Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
eText.setText(e.toString());
}}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
eText = (EditText)findViewById(R.id.uid);
eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
btnSignup = (Button)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
validation();
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(login.this,signup.class);
startActivity(i);
}
});
}}
而且我的PHP Web服务名称login.ph
<?php
include("Config.php");
// username and password sent from Form
接收参数 $名为myUsername =和addslashes($ _ POST [ '名']);
$mypassword=addslashes($_POST['pass1']);
$sql="SELECT * FROM signup WHERE user_id='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);
如果结果匹配$名为myUsername和$输入mypassword,表行必须是1排
if($count==1)
{
echo "1";(If result found send 1 to android)
}
else
{
echo "0";(If result not found send o to android)
}
?>
配置文件都连接参数来建立与数据库的连接是MySQL和我使用WAMP服务器。
* 的config.php *
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "kse";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or
die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd)
or
die("Opps some thing went wrong");
?>
当我运行我的应用程序它给我敬酒messgae如果我给错误日志中的细节,因为我用,如果与人在这里
if(res.equals("1")) { Toast.makeText(getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT).show(); Intent i = new Intent(login.this,order_pushing.class); startActivity(i); } else if(res.equals("0")) { Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT).show(); }
但是,当我给正确的信息,没有response..Please帮我。我只是个初学者到android.If你可以改正我的合作德请。
无关您的问题,但我必须评论其他任何人可能会遇到这从谷歌或其他地方: 你应该更加注意PHP侧的SQL注入。 addslashes()不幸的是没有足够的保护。我可能会推荐查看PHP数据对象(http://www.php.net/manual/en/book.pdo.php),特别是在准备好的语句中? – Sean
感谢这个信息。 –
那么现在的实际问题是什么?你能详细解释一下吗? – xDragonZ