我的程序在PHP和SQLSRV之间的Android响应错误方面存在一些问题。
问题是,当我按下注册按钮时,我无法访问“登录活动”。
PHP代码:
<?php include('connect.php');
$name = $_POST['name'];
$age = $_POST['age'];
$username = $_POST['username'];
$password = $_POST['password'];
print_r($_POST); $sql = "INSERT INTO dbo.[user] (name, age, username, password) VALUES (?, ?, ?, ?)";
$params = array(&$name, &$age, &$username, &$password);
$stmt = sqlsrv_prepare( $conn, $sql, $params);
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_execute( $stmt ) === false ) {
die( print_r( sqlsrv_errors(), true));
}
$response = array();
$response['success'] = true;
echo json_encode ($response);
sqlsrv_close($conn);
?>
Android代码:
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class Register extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etAge = (EditText) findViewById(R.id.etAge);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = etName.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final int age = Integer.parseInt(etAge.getText().toString());
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response) {
eLog.d("response", response );
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
Intent intent = new Intent(Register.this, LoginActivity.class);
Register.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(Register.this);
queue.add(registerRequest);
}
});
}
}
当我运行程序时,我收到此错误:
com.example.cursoft.loginregistra.LoginActivity$2$1.onResponse(LoginActivity.java:51)
06-22 14:42:24.125 10198-10198/com.example.cursoft.loginregistra W/System.err: at com.example.cursoft.loginregistra.LoginActivity$2$1.onResponse(LoginActivity.java:45)
06-22 14:42:24.125 10198-10198/com.example.cursoft.loginregistra W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
06-22 14:42:24.125 10198-10198/com.example.cursoft.loginregistra W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
06-22 14:42:24.125 10198-10198/com.example.cursoft.loginregistra W/System.err:
有人可以帮忙吗?