|
|
|
Android สอบถามเรื่องการค้นหาข้อมูลจาก server โดยใช้ edit text แต่ไม่สามารถนำมาแสดงผลได้ |
|
|
|
|
|
|
|
อันนี้ คือ code ที่ได้ทำค่ะ
php_get_data.php
//// $_POST["strUser"] = "ict1340"; // for Sample
//$_POST["strUser"] = "weerachai@1"; // for Sample
$strUsername = $_POST["strUser"];
$strSQL = "SELECT * FROM Room AS r, Coordinates AS c WHERE r.level_id=c.level_id AND room_name = '".$strUsername."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$intNumRows = mysql_num_rows($objQuery);
if($intNumRows==0)
{
$arr['StatusID'] = "0";
$arr['room_id'] = "0";
$arr['Error'] = "Incorrect Username and Password";
}
else
{
$arr['StatusID'] = "1";
$arr['room_id'] = $objResult["room_id"];
$arr['Error'] = "";
}
/**
$arr['StatusID'] // (0=Failed , 1=Complete)
$arr['MemberID'] // MemberID
$arr['Error' // Error Message
*/
mysql_close($objConnect);
echo json_encode($arr);
getByMemberID.php
<?php
$strMemberID = $_POST["sMemberID"];
$strSQL = "SELECT * FROM Room AS r, Coordinates AS c WHERE r.level_id=c.level_id AND room_id = '".$strMemberID."' ";
$objQuery = mysql_query($strSQL);
$obResult = mysql_fetch_array($objQuery);
if($obResult)
{
$arr["room_id"] = $obResult["room_id"];
$arr["room_name"] = $obResult["room_name"];
$arr["coordinates_x"] = $obResult["coordinates_x"];
$arr["coordinates_y"] = $obResult["coordinates_y"];
$arr["level_id"] = $obResult["level_id"];
$arr["coordinates_X1"] = $obResult["coordinates_X1"];
$arr["coordinates_Y1"] = $obResult["coordinates_Y1"];
$arr["coordinates_X2"] = $obResult["coordinates_X2"];
$arr["coordinates_Y2"] = $obResult["coordinates_Y2"];
$arr["coordinates_X3"] = $obResult["coordinates_X3"];
$arr["coordinates_Y3"] = $obResult["coordinates_Y3"];
$arr["coordinates_X4"] = $obResult["coordinates_X4"];
$arr["coordinates_Y4"] = $obResult["coordinates_Y4"];
}
mysql_close($objConnect);
/*** return JSON by MemberID ***/
/* Eg :
{"MemberID":"2",
"Username":"adisorn",
"Password":"adisorn@2",
"Name":"Adisorn Bunsong",
"Tel":"021978032",
"Email":"[email protected]"}
*/
echo json_encode($arr);
?>
Room_search
package com.example.navigationofup;
import android.os.Bundle;
import android.app.Activity;
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 android.app.AlertDialog;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
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 org.json.JSONException;
import org.json.JSONObject;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class Room_search extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.room_search);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
// txtUsername & txtPassword
final EditText txtUser = (EditText) findViewById(R.id.txtUsername);
final EditText txtPass = (EditText)findViewById(R.id.txtUsername1);
// btnLogin
final Button btnLogin = (Button) findViewById(R.id.btnLogin);
// Perform action on click
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://pampas.site11.com/php_get_data.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("strUser", txtUser.getText()
.toString()));
params.add(new BasicNameValuePair("strPass", txtPass.getText()
.toString()));
/**
* Get result from Server (Return the JSON Code) StatusID = ?
* [0=Failed,1=Complete] MemberID = ? [Eg : 1] Error = ? [On
* case error return custom error message]
*
* Eg Login Failed = {"StatusID":"0","MemberID":"0","Error":
* "Incorrect Username and Password"} Eg Login Complete =
* {"StatusID":"1","MemberID":"2","Error":""}
*/
String resultServer = getHttpPost(url, params);
/*** Default Value ***/
String strStatusID = "0";
String strMemberID = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strMemberID = c.getString("room_id");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Login
if (strStatusID.equals("0")) {
// Dialog
ad.setTitle("Error! ");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
ad.setMessage(strError);
ad.show();
txtUser.setText("");
txtPass.setText("");
} else {
Toast.makeText(Room_search.this, "กรอกถูกต้อง",
Toast.LENGTH_SHORT).show();
Intent newActivity = new Intent(Room_search.this,
DetailActivity.class);
newActivity.putExtra("MemberID", strMemberID);
startActivity(newActivity);
}
}
});
}
public String getHttpPost(String url, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
DetailActivity
public class DetailActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
showInfo();
// btnBack
final Button btnBack = (Button) findViewById(R.id.btnBack);
// Perform action on click
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(DetailActivity.this,Room_search.class);
startActivity(newActivity);
}
});
}
public void showInfo()
{
// txtMemberID,txtMemberID,txtUsername,txtPassword,txtName,txtEmail,txtTel
final TextView tMemberID = (TextView)findViewById(R.id.txtMemberID);
final TextView tUsername = (TextView)findViewById(R.id.txtUsername);
final TextView tPassword = (TextView)findViewById(R.id.txtPassword);
final TextView tName = (TextView)findViewById(R.id.txtName);
final TextView tEmail = (TextView)findViewById(R.id.txtEmail);
final TextView tTel = (TextView)findViewById(R.id.txtTel);
String url = "https://www.thaicreate.com/android/getByMemberID.php";
Intent intent= getIntent();
final String MemberID = intent.getStringExtra("MemberID");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sMemberID", MemberID));
/** Get result from Server (Return the JSON Code)
*
* {"MemberID":"2","Username":"adisorn","Password":"adisorn@2","Name":"Adisorn Bunsong","Tel":"021978032","Email":"[email protected]"}
*/
String resultServer = getHttpPost(url,params);
String strMemberID = "";
String strUsername = "";
String strPassword = "";
String strName = "";
String strEmail = "";
String strTel = "";
JSONObject c;
try {
c = new JSONObject(resultServer);
strMemberID = c.getString("room_id");
strUsername = c.getString("room_name");
if(!strMemberID.equals(""))
{
tMemberID.setText(strMemberID);
tUsername.setText(strUsername);
}
else
{
tMemberID.setText("-");
tUsername.setText("-");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ปัญหาคือพอกรอกข้อมูลที่ค้นหาลงช่องค้นหา มันไม่แสดงข้อมูลที่เราต้องการมาโชว์อ่ะค่ะ โดยจะทำการดึงข้อมูลจาก server ค่ะ เราควรแก้ไข หรือเพิ่มตรงส่วนไหนบ้างอ่ะค่ะ พอดีว่าลองแก้แล้วมันก้อยังเหมือนเดิม อ่ะค่ะ รบกวนด้วยหน่ะคะ Code (Android-Java)
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2015-04-29 13:11:01 |
By :
pampas55555 |
View :
1161 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|