我正在尝试将用户注册到我的web托管SQL数据库中。java应用程序有望在将值放入SQL语句之前将其POST到web上进行格式化。
下面是我在服务器上处理POST请求的代码。
$password=$_POST["password"];
$username=$_POST["username"];
$first = $_POST["first"];
$second = $_POST["second"];
$password = sha1($password);
$query = "INSERT INTO plateusers (email, password, first, second)
VALUES ('$username','$password', '$first', '$second')";
if ($query_run = mysqli_query($mysqli_conn, $query)) {
$response["success"] = 1;
$response["message"] = "You have been registered";
die(json_encode($response));
}
else
{
$response["success"] = 0;
$response["message"] = "Invalid details";
die(json_encode($response));
}
mysql_close();
首先,我知道我的声明是公开的,但安全措施会在生效后出台。
然后,我创建了一个表单,供用户在我的RegisterActivity中输入他们的详细信息,其代码为:
public class RegisterActivity extends ActionBarActivity {
Context c;
EditText eTEmail;
EditText eTPassword;
EditText eTFname;
EditText eTSname;
ImageButton iBLogin;
String password;
String email;
String fname;
String sname;
String url = "*******";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
setContentView(R.layout.activity_register);
//Casting
eTEmail = (EditText) findViewById(R.id.eTEmail);
eTPassword = (EditText) findViewById(R.id.eTPassword);
eTFname = (EditText) findViewById(R.id.eTFname);
eTSname = (EditText) findViewById(R.id.eTSname);
iBLogin = (ImageButton) findViewById(R.id.iBLogin);
iBLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// _("Login button hit");
email = eTEmail.getText() + "";
fname = eTFname.getText() + "";
sname = eTSname.getText() + "";
password = eTPassword.getText() + "";
if (sname.length() == 0 || fname.length() == 0 || email.length() == 0 || password.length() == 0) {
Toast.makeText(c, "Please fill in all fields", Toast.LENGTH_SHORT).show();
return;
}
if (sname.length() > 0 && fname.length() > 0 && email.length() > 0 && password.length() > 0) {
//Do networking
Networking n = new Networking();
n.execute(url, Networking.NETWORK_STATE_REGISTER);
}
}
});
}
//AsyncTask good for long running tasks
public class Networking extends AsyncTask {
public static final int NETWORK_STATE_REGISTER = 1;
@Override
protected Object doInBackground(Object[] params) {
getJson((String) params[0], (Integer) params[1]);
return null;
}
}
private void getJson(String url, int state) {
//Do a HTTP POST, more secure than GET
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
boolean valid = false;
switch (state) {
case Networking.NETWORK_STATE_REGISTER:
//Building key value pairs to be accessed on web
postParameters.add(new BasicNameValuePair("username", email));
postParameters.add(new BasicNameValuePair("password", password));
postParameters.add(new BasicNameValuePair("first", fname));
postParameters.add(new BasicNameValuePair("second", sname));
valid = true;
break;
default:
// Toast.makeText(c, "Unknown state", Toast.LENGTH_SHORT).show();
}
if (valid == true) {
//Reads everything that comes from server
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer("");
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);
//Send off to server
HttpResponse response = httpClient.execute(request);
//Reads response and gets content
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
String LineSeparator = System.getProperty("line.separator");
//Read back server output
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();
} catch (Exception e) {
//Toast.makeText(c, "Error during networking", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
decodeResultIntoJson(stringBuffer.toString());
//Toast.makeText(c, "Valid details", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(c, "Invalid details", Toast.LENGTH_SHORT).show();
}
}
private void decodeResultIntoJson(String response) {
/* Example from server
{
"success":1,
"message":"You have been successfully registered"
}
*/
if (response.contains("error")) {
try {
JSONObject jo = new JSONObject(response);
String error = jo.getString("error");
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
JSONObject jo = new JSONObject(response);
String success = jo.getString("success");
String message = jo.getString("message");
// Toast.makeText(c, "Register successful", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我第一次尝试开发Android应用程序,任何帮助都将不胜感激。
Url变量已被注释掉,原因很明显,它链接到上面提到的php脚本。
当运行时,似乎没有添加到数据库中,但是单独运行脚本将允许输入到数据库中。我认为张贴数据时存在问题
确保我的应用程序可以连接到互联网解决了这个问题。直到我找到合适的代码来解决这个问题,我才知道自己遇到了这个问题。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />