2017-05-25 82 views
0

我正在製作一個我需要登錄和註冊的Android應用程序,我是一名編程初學者,我做了一些不顯示任何結果或錯誤的工作。 `在Android Web服務中需要幫助

package com.example.user.appliication; 

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

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 

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

import java.util.HashMap; 

public class RegisterActivity extends AppCompatActivity { 

EditText ETname, ETusername, ETpassword, ETconfpas, ETrollnumber, ETemail; 
Button RegisterButton; 
StringRequest request; 
RequestQueue queue; 
String URL = "http://user.000webhostapp.com/folder/script.php"; 

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



    ETname = (EditText) findViewById(R.id.etName); 
    ETusername = (EditText) findViewById(R.id.etUsername); 
    ETpassword = (EditText) findViewById(R.id.etPassword); 
    ETconfpas = (EditText) findViewById(R.id.etConPass); 
    ETrollnumber = (EditText) findViewById(R.id.etRollnumber); 
    ETemail = (EditText) findViewById(R.id.etEmail); 

    queue = Volley.newRequestQueue(this); 

    RegisterButton = (Button) findViewById(R.id.regButton); 
    RegisterButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if(ETname.getText().toString().matches("")||ETusername.getText().toString().matches("")||ETpassword.getText().toString().matches("")||ETconfpas.getText().toString().matches("")||ETrollnumber.getText().toString().matches("")||ETemail.getText().toString().matches("")){ 
       Toast.makeText(RegisterActivity.this, "All fields required", Toast.LENGTH_SHORT).show(); 
      } 
      else{ 
       request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String s) { 

         try { 
          JSONObject jsonObject = new JSONObject(s); 
          boolean success = jsonObject.getBoolean("success"); 
          if(success){ 
           String message = jsonObject.getString("message"); 
           Intent i = new Intent(RegisterActivity.this, HomeActivity.class); 
           startActivity(i); 
           Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show(); 

          } 
          else { 
           String message = jsonObject.getString("message"); 
           Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError volleyError) { 
         Toast.makeText(RegisterActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       }){ 
        @Override 
        protected HashMap<String, String> getParams(){ 
         HashMap<String,String> params = new HashMap<String, String>(); 
         params.put("name",ETname.getText().toString()); 
         params.put("username",ETusername.getText().toString()); 
         params.put("password",ETpassword.getText().toString()); 
         params.put("rollnumber",ETrollnumber.getText().toString()); 
         params.put("email",ETemail.getText().toString()); 
         return params; 
        } 
       }; 
       queue.add(request); 
      } 
     } 
    }); 
} 
} ` 

爲registeration PHP腳本是

<?php 
$response = array(); 
if (isset($_POST['user_name']) && isset($_POST['user_password']) && 
isset($_POST['user_fullname'])) { 
// include db connect 
require_once __DIR__ . '/connection.php'; 

$name = $_POST['name']; 
$username = $_POST['username']; 
$password = $_POST['password']; 
$rollnumber = $_POST['rollnumber']; 
$email = $_POST['email']; 

$query = "INSERT INTO appusers(name ,username, password, rollnumber, email) 
VALUES ('$name','$username','$password','$rollnumber','$email')"; 
// insert into db 
$result = mysqli_query($con,$query); 

//check if registration completed 
if ($result) { 
    $response["success"] = true; 
    $response["message"] = "Registration completed"; 

    // echo JSON response 
    echo json_encode($response); 
} 
else{ 
    $response["success"] = false; 
    $response["message"] = "Rollnumber alreaday exist"; 

    // echo JSON response 
    echo json_encode($response); 
} 

} 
else{ 
$response["success"] = false; 
$response["message"] = "Something went wrong"; 

// echo JSON response 
echo json_encode($response); 
} 
?> 

亞行停止,不顯示在我運行此應用程序的任何錯誤響應,並沒有將數據發送到我的數據庫。

+0

強烈建議你看看準備的語句,因爲你當前打開的SQL注入。 –

回答

0

我從來沒有使用StringRequest你試過沒有把它放在主線程使用AsyncTask? 它應該是這樣的:

在您按一下按鈕:

//Trust me you need a progress bar 
pb = (ProgressBar) this.findViewById(R.id.regProgressBar); 
RegisterButton = (Button) findViewById(R.id.regButton); 
    RegisterButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LoginLoader ll = new LoginLoader(pb, RegisterActivity.this); 
      String[] args = new String[6]; 
      args[0] = URL; 
      args[1] = ETname.getText().toString(); 
      args[2] = ETusername.getText().toString(); 
      args[3] = ETemail.getText().toString(); 
      args[4] = ETpassword.getText().toString(); 
      args[5] = ETrollnumber.getText().toString(); 
      ll.execute(args); 
    }); 

public class LoginLoader extends AsyncTask<String, Void, String> { 
    private final ProgressBar _pb; 
    private final Activity _act; 

    public LoginLoader(ProgressBar pb, Activity act) 
    { 
     _pb = pb; 
     _act = act; 

    } 
    @Override 
    protected String doInBackground(String... params) { 
     try { 
      String url = params[0]; 
      HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); 
      con.setDoInput(true); 
      con.setRequestMethod("POST"); 

      if(params.length > 1) { 
       con.setDoOutput(true); 

       OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());   
writer.write("name=" + URLEncoder.encode(params[1], "UTF-8") + "&"); 
writer.write("username=" + URLEncoder.encode(params[2], "UTF-8") + "&"); 
writer.write("email=" + URLEncoder.encode(params[3], "UTF-8") + "&"); 
writer.write("password=" + URLEncoder.encode(params[4], "UTF-8") + "&"); 
writer.write("rollnumber=" + URLEncoder.encode(params[5], "UTF-8") + "&"); 
       writer.close(); 
      } 
      InputStreamReader reader = new InputStreamReader(con.getInputStream()); 
      char[] buf = new char[1024]; 
      StringBuilder sb = new StringBuilder(); 
      int len = 0; 
      while((len = reader.read(buf)) >0) 
      { 
       sb.append(buf, 0, len); 
      } 
      reader.close(); 
      return sb.toString(); 
     } 
     catch (Exception ex) 
     { 
      return null; 
     } 
    } 
    @Override 
    protected void onPostExecute(String result) { 

     try { 
          JSONObject jsonObject = new JSONObject(result); 
          boolean success = jsonObject.getBoolean("success"); 
          if(success){ 
           String message = jsonObject.getString("message"); 
           Intent i = new Intent(_act, HomeActivity.class); 
           _act.startActivity(i); 
           Toast.makeText(_act, message, Toast.LENGTH_SHORT).show(); 

          } 
          else { 
           String message = jsonObject.getString("message"); 
           Toast.makeText(_act, message, Toast.LENGTH_SHORT).show(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
     _pb.setVisibility(ProgressBar.INVISIBLE); 
    } 

    @Override 
    protected void onPreExecute() { 
     _pb.setVisibility(ProgressBar.VISIBLE); 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) {} 
}