我正在使用Android中的php mysql進行登錄和註冊活動。我試着測試註冊部分,但它不工作。我的PHP文件已經過測試,並且工作正常。我懷疑我的問題可能是我提供的網址,但它可以在瀏覽器中使用。當詳細信息輸入到註冊表單中時,單擊提交按鈕不會執行任何操作。Android登錄註冊
這裏是我的代碼: 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();
}
}
您是否以正確的順序將數據發送到服務器? –
[Json在Android應用中的解析]可能的重複(http://stackoverflow.com/questions/3819273/json-parsing-in-android-application) –
是的,它是在正確的順序@RakshitNawani – guddu320