嗨,我正在从事一个大学项目,每次用户填写信息并单击注册按钮时,都会在数据库中创建一个新记录。我使用 apache 服务器、php 和 mysql 数据库以及 java。我将 php 文件保存在 apache 文件夹中,并且我使用 localhost 来调用这些文件。我还使用 jason 类来查看数据。
我的问题是:每次我填写信息并单击注册按钮时,应用程序都无法在数据库中创建记录,我到处搜索解决方案但没有找到,希望有人能帮助我。提前致谢。
这是注册页面的代码:
package tigris.codes.Registration.system;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Registration extends Activity {
//Declaring Variables
EditText StudentName,StudentEmail,StudentPassword,StudentMobNumber,StudentNumber;
Button RegisterButton;
JSONParser jsonParser = new JSONParser();
private ProgressDialog pDialog;
// url to create new product
private static String url_create_product = "http://localhost/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
//Linking Objects
StudentName = (EditText) findViewById(R.id.RegNameEditText);
StudentEmail = (EditText) findViewById(R.id.RegEmailEditText);
StudentPassword = (EditText) findViewById(R.id.RegPasswordEditText);
StudentMobNumber = (EditText) findViewById(R.id.RegPhoneEditText);
StudentNumber = (EditText) findViewById(R.id.RegStudentNumber);
RegisterButton = (Button) findViewById(R.id.registerUserButton);
//Setting Button Event
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}//End of onCreate method
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Registration.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = StudentName.getText().toString();
String password = StudentPassword.getText().toString();
String email = StudentEmail.getText().toString();
String phone = StudentMobNumber.getText().toString();
String snumber = StudentNumber.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("phone", phone));
params.add(new BasicNameValuePair("snumber", snumber));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.registration, menu);
return true;
}//end of boolean onCreateOptionMenu
}//end of main class
这些是我在 log-cat 中遇到的错误:
10-24 13:44:49.141: E/Buffer Error(1520): Error converting result java.lang.NullPointerException: lock == null
10-24 13:44:49.161: E/JSON Parser(1520): Error parsing data org.json.JSONException: End of input at character 0 of
10-24 13:44:49.361: E/AndroidRuntime(1520): FATAL EXCEPTION: AsyncTask #1
10-24 13:44:49.361: E/AndroidRuntime(1520): java.lang.RuntimeException: An error occured while executing doInBackground()
10-24 13:44:49.361: E/AndroidRuntime(1520): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-24 13:44:49.361: E/AndroidRuntime(1520): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.lang.Thread.run(Thread.java:856)
10-24 13:44:49.361: E/AndroidRuntime(1520): Caused by: java.lang.NullPointerException
10-24 13:44:49.361: E/AndroidRuntime(1520): at tigris.codes.attendance.system.Registration$CreateNewProduct.doInBackground(Registration.java:107)
10-24 13:44:49.361: E/AndroidRuntime(1520): at tigris.codes.attendance.system.Registration$CreateNewProduct.doInBackground(Registration.java:1)
10-24 13:44:49.361: E/AndroidRuntime(1520): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-24 13:44:49.361: E/AndroidRuntime(1520): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-24 13:44:49.361: E/AndroidRuntime(1520): ... 4 more
10-24 13:44:52.711: E/WindowManager(1520): Activity tigris.codes.attendance.system.Registration has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cff448 V.E..... R.....ID 0,0-329,175} that was originally added here
10-24 13:44:52.711: E/WindowManager(1520): android.view.WindowLeaked: Activity tigris.codes.attendance.system.Registration has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cff448 V.E..... R.....ID 0,0-329,175} that was originally added here
10-24 13:44:52.711: E/WindowManager(1520): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-24 13:44:52.711: E/WindowManager(1520): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-24 13:44:52.711: E/WindowManager(1520): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-24 13:44:52.711: E/WindowManager(1520): at android.app.Dialog.show(Dialog.java:281)
10-24 13:44:52.711: E/WindowManager(1520): at tigris.codes.attendance.system.Registration$CreateNewProduct.onPreExecute(Registration.java:80)
10-24 13:44:52.711: E/WindowManager(1520): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-24 13:44:52.711: E/WindowManager(1520): at android.os.AsyncTask.execute(AsyncTask.java:534)
10-24 13:44:52.711: E/WindowManager(1520): at tigris.codes.attendance.system.Registration$1.onClick(Registration.java:57)
10-24 13:44:52.711: E/WindowManager(1520): at android.view.View.performClick(View.java:4204)
10-24 13:44:52.711: E/WindowManager(1520): at android.view.View$PerformClick.run(View.java:17355)
10-24 13:44:52.711: E/WindowManager(1520): at android.os.Handler.handleCallback(Handler.java:725)
10-24 13:44:52.711: E/WindowManager(1520): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 13:44:52.711: E/WindowManager(1520): at android.os.Looper.loop(Looper.java:137)
10-24 13:44:52.711: E/WindowManager(1520): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-24 13:44:52.711: E/WindowManager(1520): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:44:52.711: E/WindowManager(1520): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 13:44:52.711: E/WindowManager(1520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-24 13:44:52.711: E/WindowManager(1520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-24 13:44:52.711: E/WindowManager(1520): at dalvik.system.NativeStart.main(Native Method)