我正在使用Android中的php mysql进行登录和注册活动。我试过测试注册部分,但它没有工作。我的php文件已经过测试,它们正常运行。我怀疑我的问题可能是我提供的网址,但它可以在浏览器中使用。在注册表单中输入详细信息后,单击“提交”按钮不会执行任何操作。
这是我的代码: Login.java
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void userLogin(View view)
{
}
public void userReg(View view)
{
startActivity(new Intent(this, Registration.class));
}
}
Registration.java
public class Registration extends Activity {
EditText ETname, ETusername, ETpassword;
String name, username, userpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
ETname = (EditText)findViewById(R.id.newUsername);
ETusername = (EditText)findViewById(R.id.newUserid);
ETpassword = (EditText)findViewById(R.id.newUserpassword);
}
public void reg(View view)
{
name=ETname.getText().toString();
username=ETusername.getText().toString();
userpass=ETpassword.getText().toString();
String method= "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,name,username,userpass);
finish();
}
}
BackgroundTask.java
public class BackgroundTask extends AsyncTask<String, Void, String> {
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;
}
@Override
protected String doInBackground(String... params)
{
String reg_url="http://127.0.0.1/webapp/register.php";
String login_url="http://127.0.0.1/webapp/login.php";
String method= params[0];
if(method.equals("register"))
{
String name=params[1];
String user_name=params[2];
String user_pass=params[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8")+"="+URLEncoder.encode(user_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream is= httpURLConnection.getInputStream();
is.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
您可能不需要将KEY
转换为encode
,只需将值encoded
转换为String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8")+"="+URLEncoder.encode(user_pass,"UTF-8");
所以,
更改此
String data = "user"="+URLEncoder.encode(name,"UTF-8")+"&"+
"user_name"+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+"user_pass"+"="+URLEncoder.encode(user_pass,"UTF-8");
要
<form action="YourPageName.aspx" method="post">
<input name="TextBox1" type="text" value="" id="TextBox1" />
<input name="TextBox2" type="password" id="TextBox2" />
<input type="submit" name="Button1" value="Button" id="Button1" />
</form>
看看这是否适合您。
答案 1 :(得分:0)
你说你点击了提交按钮,也许你没有正确调用onClick。 您是否尝试通过findViewById(R.id.button)获取活动按钮的引用? 同时尝试打印网址以确保格式正确。