Android注册表单使用HTTP Post、Java、Json、PHP
我正在尝试向我的web托管SQL数据库注册用户。java应用程序希望将这些值发布到web上,以便在放入SQL语句之前进行格式化 下面是我在服务器上处理POST请求的代码Android注册表单使用HTTP Post、Java、Json、PHP,java,php,android,json,post,Java,Php,Android,Json,Post,我正在尝试向我的web托管SQL数据库注册用户。java应用程序希望将这些值发布到web上,以便在放入SQL语句之前进行格式化 下面是我在服务器上处理POST请求的代码 $password=$_POST["password"]; $username=$_POST["username"]; $first = $_POST["first"]; $second = $_POST["second"]; $password = sha1($password); $query = "INSERT IN
$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();
}
}
}
公共类注册活动扩展了ActionBarActivity{
上下文c;
编辑文本和电子邮件;
编辑文本和密码;
编辑文本eTFname;
编辑文本eTSname;
ImageButton-iBLogin;
字符串密码;
字符串电子邮件;
字符串fname;
弦圈套;
字符串url=“*******”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
c=这个;
setContentView(R.layout.activity\u寄存器);
//铸造
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(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//_uu(“点击登录按钮”);
email=eTEmail.getText()+“”;
fname=eTFname.getText()+“”;
sname=eTSname.getText()+“”;
password=eTPassword.getText()+“”;
如果(sname.length()==0 | | fname.length()==0 | | | email.length()==0 | | | password.length()==0){
Toast.makeText(c,“请填写所有字段”,Toast.LENGTH_SHORT.show();
回来
}
如果(sname.length()>0&&fname.length()>0&&email.length()>0&&password.length()>0){
//建立网络
网络n=新网络();
n、 执行(url、网络、网络状态、注册);
}
}
});
}
//AsyncTask适用于长时间运行的任务
公共类网络扩展异步任务{
公共静态最终整数网络状态寄存器=1;
@凌驾
受保护对象doInBackground(对象[]参数){
getJson((字符串)参数[0],(整数)参数[1]);
返回null;
}
}
私有void getJson(字符串url,int状态){
//执行HTTP POST,比GET更安全
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost请求=新的HttpPost(url);
List postParameters=new ArrayList();
布尔有效=假;
开关(状态){
案例网络。网络状态注册:
//构建要在web上访问的键值对
添加(新的BasicNameValuePair(“用户名”,电子邮件));
添加(新的BasicNameValuePair(“密码”,password));
添加(新的BasicNameValuePair(“第一个”,fname));
添加(新的BasicNameValuePair(“第二个”,sname));
有效=真;
打破
违约:
//Toast.makeText(c,“未知状态”,Toast.LENGTH_SHORT).show();
}
如果(有效==true){
//读取来自服务器的所有内容
BufferedReader BufferedReader=null;
StringBuffer StringBuffer=新的StringBuffer(“”);
试一试{
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(后参数);
请求。设置实体(实体);
//发送到服务器
HttpResponse response=httpClient.execute(请求);
//读取响应并获取内容
bufferedReader=新的bufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
字符串LineSeparator=System.getProperty(“line.separator”);
//回读服务器输出
而((line=bufferedReader.readLine())!=null){
stringBuffer.append(行+行分隔符);
}
bufferedReader.close();
}捕获(例外e){
//Toast.makeText(c,“网络连接时出错”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}
decodeResultIntoJson(stringBuffer.toString());
//Toast.makeText(c,“有效细节”,Toast.LENGTH_SHORT).show();
}否则{
//Toast.makeText(c,“无效细节”,Toast.LENGTH_SHORT).show();
}
}
私有void decodeResultIntoJson(字符串响应){
/*来自服务器的示例
{
"成功":一,,
“消息”:“您已成功注册”
}
*/
if(response.contains(“error”)){
试一试{
JSONObject jo=新的JSONObject(响应);
字符串错误=jo.getString(“错误”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
试一试{
JSONObject jo=新的JSONObject(响应);
String success=jo.getString(“success”);
字符串消息=jo.getString(“消息”);
//Toast.makeText(c,“注册成功”,Toast.LENGTH_SHORT).show();
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
这是我第一次尝试开发Android应用程序,任何帮助都将不胜感激
Url变量被注释掉的原因很明显,它链接到上面提到的php脚本
当运行时,似乎没有添加到数据库中,但是单独运行脚本将允许输入到数据库中。我认为发布数据时存在问题,确保我的应用程序可以连接到internet解决了问题。这是一个我不知道我有问题,直到我找到合适的代码来解决这个问题
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
您确定您的PHP脚本正在获取POST数据吗?您的应用程序中到底有什么不起作用?首先尝试将用户注册到数据库