2016-03-04 285 views
-1

我正在使用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(); 
} 

}

+0

您是否以正確的順序將數據發送到服務器? –

+0

[Json在Android應用中的解析]可能的重複(http://stackoverflow.com/questions/3819273/json-parsing-in-android-application) –

+0

是的,它是在正確的順序@RakshitNawani – guddu320

回答

0

你可能不需要轉換您KEYencode只值需要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"); 

看看這爲你工作。

0

你說你點擊提交按鈕,也許你沒有正確地調用onClick。 您是否嘗試通過findViewById(R.id.button)獲取對Activity中按鈕的引用? 也嘗試打印網址以確保它的格式正確。