java连接wamp_模拟器连接到wampserver

我正在开发一个登录界面(android) . 我曾经将我的模拟器连接到数据库MySQL . 但它不起作用 . 总是显示用户甚至没有找到我插入它 . 这是我的模拟器的图片:

OInIM.png

build.gradle:apply plugin:'com.android.application'

android {compileSdkVersion 23 buildToolsVersion“23.0.2”useLibrary'org.apache.http.legacy'

defaultConfig {

applicationId "com.example.mouna.androidproject"

minSdkVersion 15

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

依赖{compile fileTree(dir:'libs',include:['* .jar'])testCompile'junit:junit:4.12'compile'com.android.support:appcompat-v7:23.1.1'compile'com.android . 支持:设计:23.1.1'

}

AndroidPHPConnectionDemo.java:

package com.example.mouna.androidproject;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.BasicResponseHandler;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

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.TextView;

import android.widget.Toast;

public class AndroidPHPConnectionDemo extends Activity {

Button b;

EditText et,pass;

TextView tv;

HttpPost httppost;

StringBuffer buffer;

HttpResponse response;

HttpClient httpclient;

List nameValuePairs;

ProgressDialog dialog = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

b = (Button)findViewById(R.id.Button01);

et = (EditText)findViewById(R.id.username);

pass= (EditText)findViewById(R.id.password);

tv = (TextView)findViewById(R.id.tv);

b.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

dialog = ProgressDialog.show(AndroidPHPConnectionDemo.this, "",

"Validating user...", true);

new Thread(new Runnable() {

public void run() {

login();

}

}).start();

}

});

}

void login(){

try{

httpclient=new DefaultHttpClient();

httppost= new HttpPost("http://10.0.2.2/my_folder_inside_htdocs/check.php");

//add your data

nameValuePairs = new ArrayList(2);

nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];

nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

//Execute HTTP Post Request

response=httpclient.execute(httppost);

// edited by James from coderzheaven.. from here....

ResponseHandler responseHandler = new BasicResponseHandler();

final String response = httpclient.execute(httppost, responseHandler);

System.out.println("Response : " + response);

runOnUiThread(new Runnable() {

public void run() {

tv.setText("Response from PHP : " + response);

dialog.dismiss();

}

});

if(response.equalsIgnoreCase("User Found")){

runOnUiThread(new Runnable() {

public void run() {

Toast.makeText(AndroidPHPConnectionDemo.this,"Login Success", Toast.LENGTH_SHORT).show();

}

});

startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));

}else{

showAlert();

}

}catch(Exception e){

dialog.dismiss();

System.out.println("Exception : " + e.getMessage());

}

}

public void showAlert(){

AndroidPHPConnectionDemo.this.runOnUiThread(new Runnable() {

public void run() {

AlertDialog.Builder builder = new AlertDialog.Builder(AndroidPHPConnectionDemo.this);

builder.setTitle("Login Error.");

builder.setMessage("User not Found.")

.setCancelable(false)

.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

}

});

AlertDialog alert = builder.create();

alert.show();

}

});

}

}

userpage.java:package com.example.mouna.androidproject;

import android.app.Activity;

import android.os.Bundle;

public class UserPage extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.userpage);

}

}

php文件:

mysqli_select_db($localhost,$database_localhost);

if (isset($_POST['username'])&& isset($_POST['password'])) {

$username = $_POST['username'];

$password = $_POST['password'];

$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";

$query_exec = mysqli_query($localhost,$query_search) or die(mysqli_error());

while($row=mysqli_fetch_assoc($query_exec)){

$output[]=$row;}

print(json_encode($output));

mysqli_close();

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值