package com.example.login;
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.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
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;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog =null;
@SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);}
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) {
// TODO Auto-generated method stub
dialog=ProgressDialog.show(MainActivity.this,"", "Validating user...", true);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
login();
}
}).start();
}
});
}
void login() {
try {
httpclient=new DefaultHttpClient();
httppost=new HttpPost("http://222.104.152.143:80/check.php");
nameValuePairs =new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response:"+response);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//tv.setText("response from php:"+response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Login success", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,UserPage.class));
}
});
startActivity(new Intent(MainActivity.this,UserPage.class));
finish();
}else {
Toast.makeText(MainActivity.this,"Login Fail", Toast.LENGTH_SHORT).show();
}
}catch(Exception e) {
dialog.dismiss();
System.out.println("Exception:"+e.getMessage());
}
}
}
===================================================
php 부분 입닏.
<?php
$hostname_localhost ="127.0.0.1:3306";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="123456";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "No Such User Found";
echo "연결실패";
}
else {
echo "User Found";
}
?>
php와 mysql은 연동이 됫지만 안드로이드에서 는 도통 연결이 되지 않습니다.
mysql에 데이터가 잇지만 no such user found 이게 계속 뜨던데 php와 mysql 연동에 관한 문제일까요??
도대체 어떤 문제일까요