|
|
|
สอบถามปัญหาการเขียน Android เชื่อมต่อ Server แต่ใน localhost ใช้งานได้ปกติ |
|
|
|
|
|
|
|
ปัญหามีอยู่ว่าผมเขียน Android ให้เชื่อมต่อกับฐานข้อมูล แล้วพอเรากดส่งค่าไปที่ PHP บน Server ให้มันส่งค่ากลับมา ทำไม Server ถึงไม่ส่งค่ากลับมาครับทั้งๆ ที่ PHP ที่อยู่บนเครื่องลองใช้งานผ่าน LOCALHOST ใช้งานได้ปกติ
Code (Android-Java)
package com.example.tfza.ohotime;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class SchoolResetPasswordActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
TextView tv1,tv2,tv3;
ImageView imv;
private static final String TAG = "myApp";
TextView tv;
EditText TPasswordBe,TPasswordAf,TPasswordAf1;
String PasswordBe,PasswordAf,PasswordAf1,email,Database,url;
ProgressDialog pDialog;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_reset_password);
nav();
url= "http://www.test.com/ionicPHP/testandroid/SchoolResetPassword.php";
email = getIntent().getExtras().getString("email");
Database = getIntent().getExtras().getString("database");
}
private void nav() {
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
ActionBar appbar = getSupportActionBar();
appbar.setTitle("เปลี่ยนรหัสผ่าน");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_homepage) {
// Handle the camera action
Thread myThread = new Thread(){
@Override
public void run(){
try {
sleep(0);
Intent intent = new Intent(getApplication(),SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
finish();
}catch (InterruptedException e){
e.printStackTrace();
}
}};
myThread.start();
} else if (id == R.id.nav_holiday) //วันหยุด
{
Thread myThread = new Thread(){
@Override
public void run(){
try {
sleep(0);
Intent intent = new Intent(getApplication(),SchoolHolidayActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
finish();
}catch (InterruptedException e){
e.printStackTrace();
}
}};
myThread.start();
} else if (id == R.id.nav_leave) //ลา
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}else if (id == R.id.nav_student)//นักเรียน
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
else if (id == R.id.nav_result) //สรุป
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
else if (id == R.id.nav_gradeAdd)//เพิ่มเกรด
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
else if (id == R.id.nav_grade)//เกรด
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
else if (id == R.id.nav_inout) //เวลาเข้าออก
{
Intent intent = new Intent(this,SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
else if (id == R.id.nav_password)//เปลี่ยนรหัสผ่าน
{
Intent intent = new Intent(this,SchoolResetPasswordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void btnSave(View view) {
TPasswordBe =(EditText) findViewById(R.id.edPasswordBe);
TPasswordAf =(EditText) findViewById(R.id.edPasswordAf);
TPasswordAf1 =(EditText) findViewById(R.id.edPasswordAf1);
PasswordBe = TPasswordBe.getText().toString().trim();
PasswordAf = TPasswordAf.getText().toString().trim();
PasswordAf1 = TPasswordAf1.getText().toString().trim();
if(!PasswordBe.isEmpty() && !PasswordAf.isEmpty()&& !PasswordAf1.isEmpty()){
//requestLogin(userID,passWd);
if(PasswordAf.equals(PasswordAf1)){
if(!PasswordBe.equals(PasswordAf)){
/*Context context=getApplicationContext();
Toast.makeText(context,"ไม่ตรง ",Toast.LENGTH_LONG).show();*/
ProcessData(PasswordBe,PasswordAf,email,Database);
}else {
//
Context context=getApplicationContext();
Toast.makeText(context,"รหัสผ่านเดิมไม่ควรตรงกับรหัสผ่านใหม่ กรุณาตรสจสอบ",Toast.LENGTH_LONG).show();
}
}else{
Context context=getApplicationContext();
Toast.makeText(context,"ยืนยันรหัสผ่านไม่ตรงกัน กรุณาตรสจสอบ",Toast.LENGTH_LONG).show();
}
}else{
Context context=getApplicationContext();
Toast.makeText(context,"กรุณากรอก ข้อมูลให้ครบ ",Toast.LENGTH_LONG).show();
}
}
private void ProcessData(String passwordBe, String passwordAf, String email) {
}
private void ProcessData(final String passwordBe, final String passwordAf, final String email, final String Database) {
pDialog =new ProgressDialog(this);
pDialog.setMessage("กรุณรอสักครู่ ...");
pDialog.show();
/*Context context=getApplicationContext();
Toast.makeText(context,"กรอก สำเร็จ ",Toast.LENGTH_SHORT).show();
*/
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
int returnCode = jObj.getInt("code");
String returnMsg = jObj.getString("return");
//String passwordBe = jObj.getString("passwordBe");
//String passwordAf = jObj.getString("passwordAf");
Log.d(TAG, String.valueOf(returnCode) + returnMsg);
if (returnCode == 1) {
Context context=getApplicationContext();
Toast.makeText(context,returnMsg,Toast.LENGTH_LONG).show();
/*Intent intent = new Intent(getBaseContext(), OrganizationIndexActivity.class);
intent.putExtra("email", emailMsg);
startActivity(intent);*/
Thread myThread = new Thread(){
@Override
public void run(){
try {
sleep(0);
Intent intent = new Intent(getApplication(),SchoolIndexActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String email = getIntent().getExtras().getString("email");
String Database = getIntent().getExtras().getString("database");
//Toast.makeText(context,"Email "+email,Toast.LENGTH_LONG).show();
intent.putExtra("database", Database);
intent.putExtra("email", email);
startActivity(intent);
finish();
}catch (InterruptedException e){
e.printStackTrace();
}
}};
myThread.start();
} else {
Context context=getApplicationContext();
Toast.makeText(context,returnMsg,Toast.LENGTH_LONG).show();
//tv.setText(returnMsg);
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Context context=getApplicationContext();
Toast.makeText(context,"OnError : "+error.getMessage(),Toast.LENGTH_SHORT).show();
pDialog.hide();
}
})
{
@Override
protected Map<String,String> getParams(){
Map<String,String>params=new HashMap<String,String>();
params.put("passwordBe",passwordBe);
params.put("passwordAf",passwordAf);
//params.put("passwordAf1",passwordAf1);
params.put("email",email);
params.put("databese",Database);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
}
Code (PHP)
<?php
$passwordBe=$_REQUEST['passwordBe'];
$passwordAf=$_REQUEST['passwordAf'];
//$passwordAf1=$_POST['passwordAf1'];
$email=$_REQUEST['email'];
$databese=$_REQUEST['databese'];
require 'config.php';
mysql_select_db('ohotime_mscan');
$sql = "SELECT * FROM namet WHERE n_mail='$email' ";
$result = mysql_query($sql);
$numrow = mysql_num_rows($result);
$value = mysql_fetch_array($result);
if ($value){
//$arr["email"] = $value["n_mail"];
if($passwordBe==$value["n_pass"]){
$sql1 = "UPDATE namet SET n_pass='$passwordAf' WHERE n_mail='$email'";
$result1 = mysql_query($sql1);
if ($result1) { $arr = array('code' =>'1' ,'return' =>'เปลี่ยนรหัสผ่านเรียบร้อย'); }
}else{
$arr = array('code' =>'2' ,'return' =>'รหัสผ่านเดิมของคุณไม่ถูกต้อง');
}
}else{
$arr = array('code' =>'2' ,'return' =>'เชื่อมต่อฐานข้อมูลไม่ได้');
}
echo json_encode($arr);
?>
ปล.รันผ่าน Xampp ใช้งานได้ปกติ แต่ บน Server ไม่ส่งค่ากลับมา
Tag : Mobile
|
|
|
|
|
|
Date :
2017-12-08 09:54:26 |
By :
โปรแกรมเมอร์มือสำรอง |
View :
1738 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
วัตถุประสงค์ของ admin คือให้ทดลองจาก browser ดูก่อนครับ ว่าเรียก จาก server ตรงๆ ได้ไหม
โค๊ด php error หรือเปล่า ถ้าไม่ ก็ค่อยตรวจสอบโค๊ด java ของ android อีกที ว่า error ตรงไหน
|
|
|
|
|
Date :
2017-12-08 15:38:27 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|