我正在尝试使用PDO从数据库中获取数据(我在这里是noob) 它正在我的本地主机上工作,但是当我尝试使用实际的服务器时,它不起作用,我不知道为什么coz其他文件工作正常,我使用相同的代码,只需编辑一些值
这是java代码
public class Attendance extends Activity {
private ProgressDialog mProgressDialog;
Button btnSearch;
EditText searchField;
TextView tv2, tv3, tv4, tv5, statVal, loginVal, logoutVal, accVal;
String stat,login,logout,acc;
JSONParser jsonParser = new JSONParser();
String username;
SessionManager session;
ArrayList<HashMap<String, String>> contactsList;
//private static String url_search_attendance = "http://10.0.3.2/sunshine-ems/attendance_details.php";
private static String url_search_attendance = "http://www.sunshine-ems.balay-indang/mobile/attendance_details.php";
Button btnLogout;
final Calendar myCalendar = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attend);
session = new SessionManager(getApplicationContext());
username = session.getUsername();
//search editText
searchField = (EditText) findViewById(R.id.editText1);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
searchField.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(Attendance.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
//Logout Button
btnLogout = (Button) findViewById(R.id.btnLogout);;
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching All products Activity
session.logoutUser();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
//Hide TextViews
instanciateAndHideTextViews();
// Search Button
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new getAttendanceDetails().execute();
displayTextViews();
}
});
}
private void instanciateAndHideTextViews(){
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
statVal = (TextView) findViewById(R.id.statVal);
loginVal = (TextView) findViewById(R.id.loginVal);
logoutVal = (TextView) findViewById(R.id.logoutVal);
accVal = (TextView) findViewById(R.id.accVal);
tv2.setVisibility(View.GONE);
tv3.setVisibility(View.GONE);
tv4.setVisibility(View.GONE);
tv5.setVisibility(View.GONE);
statVal.setVisibility(View.GONE);
loginVal.setVisibility(View.GONE);
logoutVal.setVisibility(View.GONE);
accVal.setVisibility(View.GONE);
}
private void displayTextViews(){
tv2.setVisibility(View.VISIBLE);
tv3.setVisibility(View.VISIBLE);
tv4.setVisibility(View.VISIBLE);
tv5.setVisibility(View.VISIBLE);
statVal.setVisibility(View.VISIBLE);
loginVal.setVisibility(View.VISIBLE);
logoutVal.setVisibility(View.VISIBLE);
accVal.setVisibility(View.VISIBLE);
}
private void updateLabel() {
String myFormat = "yyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
searchField.setInputType(InputType.TYPE_NULL);
searchField.requestFocus();
searchField.setText(sdf.format(myCalendar.getTime()));
}
private class getAttendanceDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Attendance.this);
mProgressDialog.setTitle("Loading Attendance details");
mProgressDialog.setMessage("loading....");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
protected String doInBackground(String... params) {
int success;
try {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("username", username));
params1.add(new BasicNameValuePair("date", searchField.getText().toString().trim()));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_search_attendance, "GET", params1);
// check your log for json response
Log.d("Details Attendance", json.toString());
// json success tag
success = json.getInt("success");
if (success == 1) {
// successfully received attendance details
JSONArray jsonobj = json.getJSONArray("attendance_data"); // JSON Array
// get first object from JSON Array
JSONObject a_details = jsonobj.getJSONObject(0);
// Setter
stat = a_details.optString("status");
login = a_details.optString("login_time");
logout = a_details.optString("logout_time");
acc = a_details.optString("accumulated_time");
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String jsonArray) {
// dismiss the dialog once got all details
mProgressDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
statVal.setText(stat);
loginVal.setText(login);
logoutVal.setText(logout);
accVal.setText(acc);
}
});
}
}
}
这是php代码
<?php
require("config.php");
if (isset($_GET['username'])) {
$query = "
SELECT * FROM attendance
where username= :username && date= :date";
$query_params = array(':username' => $_GET['username'],
':date' => $_GET['date']
);
//execute query
try {
$stmt = $db-> prepare($query);
$result = $stmt -> execute($query_params);
} catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt -> fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["attendance_data"] = array();
foreach($rows as $row) {
$a_data = array();
$a_data["login_time"] = $row["login_time"];
$a_data["logout_time"] = $row["logout_time"];
$a_data["accumulated_time"] = $row["accumulated_time"];
$a_data["status"] = $row["status"];
//update our repsonse JSON data
array_push($response["attendance_data"], $a_data);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "You don't have appointment history.";
die(json_encode($response));
}
}
?>
这是错误日志
02-04 10:17:06.986: E/Buffer Error(2920): Error converting result java.io.IOException: Attempted read on closed stream.
02-04 10:17:06.986: W/System.err(2920): org.json.JSONException: No value for attendance_data
02-04 10:17:06.990: W/System.err(2920): at org.json.JSONObject.get(JSONObject.java:354)
02-04 10:17:06.990: W/System.err(2920): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
02-04 10:17:06.990: W/System.err(2920): at com.example.androidtablayout.Attendance$getAttendanceDetails.doInBackground(Attendance.java:180)
02-04 10:17:06.990: W/System.err(2920): at com.example.androidtablayout.Attendance$getAttendanceDetails.doInBackground(Attendance.java:1)
02-04 10:17:06.990: W/System.err(2920): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-04 10:17:06.990: W/System.err(2920): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-04 10:17:06.990: W/System.err(2920): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-04 10:17:06.994: W/System.err(2920): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-04 10:17:06.994: W/System.err(2920): at java.lang.Thread.run(Thread.java:856)
再次,它正在我的localhost上工作,但不在实际的服务器上。我希望有一个人可以帮助我。非常感谢你