我已经制作了一个简单的登录应用程序,我已经在我的Android手机上安装了该应用程序..
其次我已经在我的笔记本电脑上创建了一个数据库。 wamp服务器 我已将手机和笔记本电脑连接到同一个wifi .. 我的问题是将这些用户名和密码传输到mysql数据库。 我也使用php页面连接android app数据来传输这个用户名密码 到mysql数据库..
我有以下代码..
1)android代码..
这是loginpage.java代码
package com.example.client_gps;
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.HttpEntity;
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 android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Loginpage extends Activity {
private static final String TAG_UNAME="username";
private static final String TAG_PWORD="password";
private static final String TAG_SUCCESS="success";
Button loginacc;
EditText uname,pword;
InputStream isr=null;
int r;
//JSONParser jsonParser=new JSONParser();
String username;
String password;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.enableDefaults();
setContentView(R.layout.loginpage);
loginacc=(Button)findViewById(R.id.login_account);
uname=(EditText)findViewById(R.id.usernametxt);
pword=(EditText)findViewById(R.id.passwordtxt);
loginacc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
username=uname.getText().toString();
password=pword.getText().toString();
Log.e("dd","username");
logindata();
}
});
}
public void logindata()
{
String result=null;
Log.e("msg","inside logindata");
HttpClient httpclient = new DefaultHttpClient();
//httpclient.geCredentialsProvider().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("root",""));
HttpPost httppost = new HttpPost("http://192.168.43.2/login.php");
Log.e("msg","clear1");
try
{
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("TAG_UNAME",username));
params.add(new BasicNameValuePair("TAG_PWORD",password));
httppost.setEntity(new UrlEncodedFormEntity(params));
Log.e("msg","clear2");
HttpResponse response = httpclient.execute(httppost);
Log.e("msg","clear3");
HttpEntity entity = response.getEntity();
isr= entity.getContent();
Log.e("msg",isr.toString());
}
catch(ClientProtocolException e)
{
}
catch(IOException e)
{
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb= new StringBuilder();
String line=null;
while((line= reader.readLine())!=null)
{
sb.append(line);
}
isr.close();
result=sb.toString();
Log.e("result",result);
r=Integer.parseInt(result);
}
catch(Exception e)
{
Log.e("error","error converting data");
}
if(r==1)
{
Intent i = new Intent(getApplication(),Homepage.class);
startActivity(i);
}
else
{
Log.e("msggg11","login failed");
}
}
}
这是php代码
来自android代码的数据被转移到php页面然后转移到数据库
问题是
在android sdk中的错误是'Name-pc'(pc的名称)doesent可以访问mysql服务器,这是在同一台笔记本电脑上..
<?php
$response=array();
{
$unm=$_POST["TAG_UNAME"];
$pwd=$_POST["TAG_PWORD"];
$con=mysqli_connect("192.168.43.2","root","root","mydb");
if(mysqli_connect_errno())
{
echo("connection failed".mysqli_connect_errno()); die();
}
else
$result=mysqli_query($con,"select * from userinfo where user_name='$unm' && password='$pwd'");
//$result=mysqli_query($con,"select * from userinfo");
if(mysqli_num_rows($result)>0)
{
$resp=1;
//$response["success"]=1;
/*while($row=mysqli_fetch_assoc($result))
{
$output[]=$row;
}*/
//echo json_encode($response);
echo json_encode($resp);
}
else
{
$resp=0;
echo json_encode($resp);
}
//print(json_encode($output));
mysqli_close($con);
}
?>