2017-09-23 57 views
1

嘿傢伙,所以我正在創建一個應用程序。允許用戶註冊到數據庫。但顯然註冊按鈕不會工作,一旦所有的細節已經寫好。任何人都可以看到問題是什麼?Android工作室 - 註冊按鈕不工作:c(相關我的sql數據庫)

ps - 我已經在清單文件中添加了Internet權限。此外我的register.php文件正在工作。我通過手動輸入信息來測試它。

我的代碼

package com.example.naveen.driver; 
 

 
import android.content.Intent; 
 
import android.support.v7.app.AlertDialog; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.Button; 
 
import android.widget.EditText; 
 

 
import com.android.volley.RequestQueue; 
 
import com.android.volley.Response; 
 
import com.android.volley.toolbox.Volley; 
 

 
import org.json.JSONException; 
 
import org.json.JSONObject; 
 

 
public class RegisterPage extends AppCompatActivity { 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_register_page); 
 

 
     final EditText rname = (EditText) findViewById(R.id.rname); 
 
     final EditText rusername = (EditText) findViewById(R.id.rusername); 
 
     final EditText rpassword = (EditText) findViewById(R.id.remail); 
 
     final EditText remail = (EditText) findViewById(R.id.rpassword); 
 
     final EditText rcarmodel = (EditText) findViewById(R.id.rcarmodel); 
 
     final EditText rcaryear = (EditText) findViewById(R.id.rcaryear); 
 
     final EditText rreg = (EditText) findViewById(R.id.rreg); 
 
     final Button rbutton = (Button) findViewById(R.id.rbutton); 
 

 
     rbutton.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View V) { 
 
       final String name = rname.getText().toString(); 
 
       final String username = rusername.getText().toString(); 
 
       final String email = remail.getText().toString(); 
 
       final String password = rpassword.getText().toString(); 
 
       final String caryear = rcarmodel.getText().toString(); 
 
       final String carmodel = rcaryear.getText().toString(); 
 
       final String carreg = rreg.getText().toString(); 
 

 
       Response.Listener<String> responseListener = new Response.Listener<String>(){ 
 

 
        @Override 
 
        public void onResponse(String response) { 
 
         try { 
 
          JSONObject jsonResponse = new JSONObject(response); 
 
          boolean success = jsonResponse.getBoolean("success"); 
 

 
          if(success) { 
 
           Intent intent = new Intent(RegisterPage.this, LoginPage.class); 
 
           RegisterPage.this.startActivity(intent); 
 
          }else{ 
 
           AlertDialog.Builder builder = new AlertDialog.Builder(RegisterPage.this); 
 
           builder.setMessage("Register Failed") 
 
             .setNegativeButton("Retry", null) 
 
             .create() 
 
             .show(); 
 
          } 
 

 
         } catch (JSONException e) { 
 
          e.printStackTrace(); 
 
         } 
 

 
        } 
 
       }; 
 

 
       RegisterRequest registerRequest = new RegisterRequest(name, username, password, email, carmodel, caryear, carreg, responseListener); 
 
       RequestQueue queue = Volley.newRequestQueue(RegisterPage.this); 
 
       queue.add(registerRequest); 
 

 

 
      } 
 
     }); 
 

 
    } 
 
}

RegisterRequest.java

package com.example.naveen.driver; 
 

 
import com.android.volley.Response; 
 
import com.android.volley.toolbox.StringRequest; 
 

 
import java.lang.reflect.Method; 
 
import java.util.HashMap; 
 
import java.util.Map; 
 

 
/** 
 
* Created by NAVEEN & CHRISTO on 21-Sep-17. 
 
*/ 
 

 
public class RegisterRequest extends StringRequest { 
 

 
    private static final String REGISTER_REQUEST_URL = "https://dipteran-thin.000webhostapp.com/Register.php"; 
 
    private Map<String, String> params; 
 

 
    public RegisterRequest(String name, String username, String password, String email, String carmodel, String caryear, String carreg, Response.Listener<String> listener){ 
 
      super(Method.POST, REGISTER_REQUEST_URL, listener, null); 
 
      params = new HashMap<>(); 
 
     params.put("name", name); 
 
     params.put("username", username); 
 
     params.put("password", password); 
 
     params.put("E-mail", email); 
 
     params.put("carmodel", carmodel); 
 
     params.put("caryear", caryear); 
 
     params.put("caryear", carreg); 
 

 
    } 
 

 
    @Override 
 
    public Map<String, String> getParams() { 
 
     return params; 
 
    } 
 
}

activity_register_page.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context="com.example.naveen.driver.RegisterPage"> 
 

 
    <EditText 
 
     android:id="@+id/rname" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:ems="10" 
 
     android:hint="Name" 
 
     android:inputType="textPersonName" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_alignLeft="@+id/rreg" 
 
     android:layout_alignStart="@+id/rreg" /> 
 

 
    <EditText 
 
     android:id="@+id/rusername" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:ems="10" 
 
     android:hint="Username" 
 
     android:inputType="textPersonName" 
 
     android:layout_below="@+id/rname" 
 
     android:layout_alignLeft="@+id/rname" 
 
     android:layout_alignStart="@+id/rname" /> 
 

 
    <Button 
 
     android:id="@+id/rbutton" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_marginTop="81dp" 
 
     android:text="Register" 
 
     android:layout_below="@+id/rreg" 
 
     android:layout_centerHorizontal="true" /> 
 

 
    <EditText 
 
     android:id="@+id/remail" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/rusername" 
 
     android:layout_alignStart="@+id/rusername" 
 
     android:layout_below="@+id/rusername" 
 
     android:ems="10" 
 
     android:hint="E-mail " 
 
     android:inputType="textPersonName" /> 
 

 
    <EditText 
 
     android:id="@+id/rpassword" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/remail" 
 
     android:layout_alignStart="@+id/remail" 
 
     android:layout_below="@+id/remail" 
 
     android:ems="10" 
 
     android:hint="Password" 
 
     android:inputType="textPassword" /> 
 

 
    <EditText 
 
     android:id="@+id/rcarmodel" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:ems="10" 
 
     android:hint="Car - Model" 
 
     android:inputType="textPersonName" 
 
     android:layout_below="@+id/rpassword" 
 
     android:layout_alignLeft="@+id/rpassword" 
 
     android:layout_alignStart="@+id/rpassword" /> 
 

 
    <EditText 
 
     android:id="@+id/rcaryear" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/rcarmodel" 
 
     android:layout_alignStart="@+id/rcarmodel" 
 
     android:ems="10" 
 
     android:hint="Car - Year" 
 
     android:inputType="textPersonName" 
 
     android:layout_below="@+id/rcarmodel" /> 
 

 
    <EditText 
 
     android:id="@+id/rreg" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:ems="10" 
 
     android:hint="Reg No" 
 
     android:inputType="textPersonName" 
 
     android:layout_below="@+id/rcaryear" 
 
     android:layout_centerHorizontal="true" /> 
 
</RelativeLayout>

+1

只是嘗試添加註冊按鈕內的logcat,看看它是否進入它或不。只需將logcat放在rButton.setOnlickListener()中即可。現在告訴我。 –

+0

對不起,我沒有得到你的意思? – user3431791

+0

看到我下面編輯的答案,並嘗試這樣做。 –

回答

0

我想我得到了你的問題,因爲你正在請求到服務器後,加入PARAMS,您需要向服務器調用之前添加PARAMS。做到這一點,並完成你的工作!

rbutton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View V) { 
      final String name = rname.getText().toString(); 
      final String username = rusername.getText().toString(); 
      final String email = remail.getText().toString(); 
      final String password = rpassword.getText().toString(); 
      final String caryear = rcarmodel.getText().toString(); 
      final String carmodel = rcaryear.getText().toString(); 
      final String carreg = rreg.getText().toString(); 

      //adding your params here will get the data to be passed in your db and make your job done 
      RegisterRequest registerRequest = new RegisterRequest(name, username, password, email, carmodel, caryear, carreg, responseListener); 
      RequestQueue queue = Volley.newRequestQueue(RegisterPage.this); 
      queue.add(registerRequest); 

      Response.Listener<String> responseListener = new Response.Listener<String>(){ 

       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 
         boolean success = jsonResponse.getBoolean("success"); 

         if(success) { 
          Intent intent = new Intent(RegisterPage.this, LoginPage.class); 
          RegisterPage.this.startActivity(intent); 
         }else{ 
          AlertDialog.Builder builder = new AlertDialog.Builder(RegisterPage.this); 
          builder.setMessage("Register Failed") 
            .setNegativeButton("Retry", null) 
            .create() 
            .show(); 
         } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }; 
     } 
    }); 

編輯

你的問題是你RegisterRequest類你用你的PARAMS添加東西的錯誤的方式。

使用這樣讓你的結果你ResgisterRequest類

使用params.add()

儘管使用這個params.put()

希望它會幫助你的!

+0

我收到「無法解決符號響應請求錯誤」RegisterRequest registerRequest = new RegisterRequest(名稱,用戶名,密碼,電子郵件,carmodel,caryear,carreg,responseListener); – user3431791

+0

好吧,我們正在開展業務,至少我們知道該按鈕正在工作。現在看看你的代碼,並檢查你的代碼發送的細節,問題只在那裏。現在我解決了您的按鈕無法使用的問題。現在通過你自己做 –

+0

@ user3431791看到我編輯的答案,並嘗試這樣做,註冊按鈕完成從我身邊,現在在您的RegisterRequest類中進行更改,並告訴我,如果它的工作 –