我正在尝试在我现有的数据库中更新名为出勤的列。基本上,我需要通过我的Android应用程序增加我的服务器上托管的数据库中的考勤列数。以下是使用Async的代码,但它在onPostExecute上返回一个nullpointer异常。
package com.example.soumya.attendance;
public class RecordData extends Activity{
Button btnStartProgress;
ProgressDialog progressBar;
Double lati;
Double longi;
// private ListView lvItem;
//private ArrayList<String> itemArrey;
//private ArrayAdapter<String> itemAdapter;
int count = 0;
int attendance_count = 0;
TextView textView;
Boolean attendance = false;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
GPSTracker gps;
private long fileSize = 0;
// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
String final_msg="";
Handler progressHandler = new Handler();
// Define the code block to be executed
private Handler mHandler = new Handler();
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
if(count<=6)
{Toast.makeText(getApplicationContext(), getDateTime(),
Toast.LENGTH_LONG).show();}
count++;
Log.d("Handlers", "Called on main thread");
// Repeat this the same runnable code block again another 2 seconds
//handler.postDelayed(runnableCode, (1 * 60) * 1000);
handler.postDelayed(runnableCode, 10 * 1000);
}
};
public void Logout()
{
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
super.onDestroy();
}
public void update()
{
final String update_url = "http://172.16.92.250/update.php";
final String attendance = "1";
final String uname = "aneesha";
class UserLoginClass extends AsyncTask<String,Void,String>
{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RecordData.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if (s.contains("success")) {
Intent intent = new Intent(RecordData.this, NewActivity.class);
//intent.putExtra(USER_NAME,username);
startActivity(intent);
} else {
Toast.makeText(RecordData.this, s, Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params)
{
try {
URL url = new URL(update_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("attendance", "UTF-8") + "=" + URLEncoder.encode(attendance, "UTF-8") + "&" +
URLEncoder.encode("uname", "UTF-8") + "=" + URLEncoder.encode(uname, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
UserLoginClass ulc = new UserLoginClass();
ulc.execute(attendance, uname);
}
// Intent intent = new Intent(this, NewActivity.class);
// this.startActivity(intent);
public void receiveMyMessage() {
Log.d("Textview", "found");
final TextView txt = (TextView)findViewById(R.id.username3);
Log.d("Textview", "Not found");
mHandler.post(new Runnable() {
@Override
public void run() {
// This gets executed on the UI thread so it can safely modify Views
if (attendance_count == count) {
txt.setText(final_msg+"\n\nYour attendance is marked!\nPlease logout");
txt.setTextColor(getResources().getColor(R.color.Green));
// BackgroundTask backgroundTask = new BackgroundTask();
// backgroundTask.execute("update","1");
Button btn = (Button) findViewById(R.id.update);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
update();
}
});
} else {
txt.setText(final_msg+"\n\nSorry, marked absent.\nPlease logout");
txt.setTextColor(getResources().getColor(R.color.Red));
// txt.setTextColor(getResources().getColor(Color.RED));
}
Button btn = (Button) findViewById(R.id.Button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logout();
}
});
}
}); }
以下是update.php
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'attendance');
define('DB_USER','root');
define('DB_PASSWORD','root');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$attendance = $POST['attendance'];
$uname = $POST['uname'];
$query = "UPDATE students SET attendance = ($attendance + attendance) WHERE uname = $uname");
result = mysqli_query($con,$query);
if(mysqli_num_rows($result) >0 )
{
$row = mysqli_fetch_assoc($result);
echo "success";
}
else
{echo "failed";}
?>
答案 0 :(得分:0)
首先,您必须更正您的PHP。你正在使用
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
和
result = mysqli_query($con,$query);
mysql_connect函数,然后尝试使用mysqli_query
msqyl_ *和mysqli_ *不兼容,不能同时使用,您可以使用:
或者您使用mysqli:
你也错过了结果变量assign中的$。
从评论中你也可以将$ query赋值改为:
$query = "UPDATE students SET attendance = ($attendance + attendance) WHERE uname = '$uname'");
并将$ POST更改为$ _POST
希望这有帮助