|
|
|
Android - ไม่สามารถเชื่อมต่อเข้ากับ database ได้ แต่เวลารันแล้วไม่ error ต้องทำยังไงครับ |
|
|
|
|
|
|
|
ส่วนอันนี้เป็น code ที่ผมไปดูมาอีกที่นึง
Code (Android-Java)
package com.ammaew.project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.json.JSONObject;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText etUser, etPass;
Button bLogin;
String username,password;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
}
private void initialise() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.etUser);
etPass = (EditText) findViewById(R.id.etPass);
bLogin = (Button) findViewById(R.id.btnLogin);
bLogin.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.1.34/dw-db/project.php");
username = etUser.getText().toString();
password = etPass.getText().toString();
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == 200){
entity = response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String retUser = jsonResponse.getString("user");
String retPass = jsonResponse.getString("pass");
if(username.equals(retUser)&& password.equals(retPass)){
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("user", username);
spedit.putString("pass", password);
spedit.commit();
Toast.makeText(getBaseContext(), "Login Success!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(),"Invalid Login Details", Toast.LENGTH_SHORT).show();
}
}
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(),"Incorrect Username or Password", Toast.LENGTH_LONG).show();
}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
และอันนี้ก็คือ php ของผม
Code (PHP)
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "ammaew";
$dbdb = "project";
$connect = mysql_connect($dbhost,$dbuser,$dbpass) or die("connection erreor...");
mysql_select_db($dbdb)or die("database selection errer");
echo("Complete");
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysql_query("SELECT * FROM project1 WHERE user='$username' AND pass ='$password'");
$num = mysql_num_rows($query);
if($num == 1){
while($list=mysql_fetch_assoc($query)){
$output = $list;
echo json_encode($output);
}
mysql_close();
}
?>
และอันนี้ก็เปนหน้าใน phpMyadmin ของผม
ตรง link url ควรจะใส่เปน localhost หรือ id address ของเครื่องครับ
|
|
|
|
|
Date :
2013-12-06 16:40:18 |
By :
moomaewz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าเปิด web browser ใน emulator แล้วสามารถเปิด url เหมือนใน pc ได้ แปลว่า android เชื่อมต่อกับ internet แล้วสามารถเชื่อมต่อกับ database ได้แล้ว ใช่ไหมคับ
|
|
|
|
|
Date :
2013-12-07 14:26:44 |
By :
moomaewz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ครับ งั้นคงจะต้องไล่ Debug ดูแล้วครับ
|
|
|
|
|
Date :
2013-12-09 06:39:55 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|