4
我正在開發一個帶eclipse的應用程序。我嘗試將用戶輸入的數據存儲在phpmyadmin的數據庫中。不幸的是,用戶點擊提交按鈕後,沒有響應,數據也沒有存儲在我的數據庫中。開發的android應用程序無法連接到phpmyadmin
這是我的java文件:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.content.res.Configuration;
public class UserRegister extends Activity {
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
RadioButton button1;
RadioButton button2;
Button button3;
int success = 0;
private static String url_register_user = "http://10.20.92.81/database/add_user.php";
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_register);
inputName = (EditText) findViewById(R.id.nameTextBox);
inputUsername = (EditText) findViewById(R.id.usernameTextBox);
inputEmail = (EditText) findViewById(R.id.emailTextBox);
inputPassword = (EditText) findViewById(R.id.pwTextBox);
Button button3 = (Button) findViewById(R.id.regSubmitButton);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputName.getText().toString();
String username = inputUsername.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (name.contentEquals("")||username.contentEquals("")||email.contentEquals("")||password.contentEquals(""))
{
AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this);
builder.setMessage(R.string.nullAlert)
.setTitle(R.string.alertTitle);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.show();
}
// creating new product in background thread
RegisterNewUser();
}
});
}
public void RegisterNewUser()
{
try
{
String name = inputName.getText().toString();
String username = inputUsername.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_register_user,
"GET", params);
// check log cat for response
Log.d("Send Notification", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
// successfully created product
Intent i = new Intent(getApplicationContext(), StudentLogin.class);
startActivity(i);
finish();
}
else
{
// failed to register
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
我的PHP文件:
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if (isset($_GET['name']) && isset($_GET['username']) && isset($_GET['email']) && isset($_GET['password'])) {
$name = $_GET['name'];
$username = $_GET['username'];
$email = $_GET['email'];
$password = $_GET['password'];
// mysql inserting a new row
$result = mysql_query("INSERT INTO register(name, username, email, password) VALUES('$name', '$username', '$email', '$password')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "You are successfully registered to MEMS.";
// echoing JSON response
echo json_encode($response);
}
else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
日誌貓如下:
11-25 10:37:46.772: I/Choreographer(638): Skipped 30 frames! The application may be doing too much work on its main thread.
非常感謝你..終於我解決了我近三週面臨的問題..謝謝! =) – user1850936
如果你得到的解決方案讓我知道please.How保存詳細信息在PHP myadmin mysql數據庫side.please幫助我 – Harsha