1

I was following this tutorial in which I was building login and registration app:

https://www.youtube.com/watch?v=T7Z4GVFaT4A

I've changed PHP and tested it by passing some arguments through address field, but when I'm clicking register button nothing happens. There are some things in OnResponse function that should be doing something, something should be added to database, but nothing happens.

I was wondering if maybe anything at all is being send from app if I'm not getting any response.

Also, what's troubling me, function OnResponse is never called, but it should be.

Instead of using some ASP I create server using WAMP. Below there are Java and PHP files that are crucial to this problem.

RegisterRequest.java

package com.example.dominik.praca;

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

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Dominik on 28.12.2016.
 */

public class RegisterRequest extends StringRequest {
    //private static final String REGISTER_REQUEST_URL = "http://kulturnik.ugu.pl/Register.php";
    //private static final String REGISTER_REQUEST_URL = "http://kulturnik.byethost3.com/Register2.php";
    private static final String REGISTER_REQUEST_URL = "http://127.0.0.1/kulturnik/Register.php";
    private Map<String, String> params;
    public RegisterRequest(String name, String username, String password, 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);
    }

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

RegisterActivity.java

package com.example.dominik.praca;

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 RegisterActivity extends AppCompatActivity {

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

        final EditText etUsername = (EditText) findViewById(R.id.etUsername);
        final EditText etPassword = (EditText) findViewById(R.id.etPassword);
        final EditText etName = (EditText) findViewById(R.id.etName);
        final Button bRegister = (Button) findViewById(R.id.bRegister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String name = etName.getText().toString();
                final String username = etUsername.getText().toString();
                final String password = etPassword.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(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                                AlertDialog.Builder builderSuccess = new AlertDialog.Builder(RegisterActivity.this);
                                builderSuccess.setMessage("Rejestracja zakończona powodzeniem")
                                        .create()
                                        .show();
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Błąd rejestracji")
                                        .setNegativeButton("Ponów", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name, username, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

And Register.php

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    $con = mysqli_connect("localhost", "root", "", "kulturnik");

    $name = $_POST["name"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "INSERT INTO user (name, username, password) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sss", $name, $username, $password);

    mysqli_stmt_execute($statement);

    $response = array();
    $response["success"] = true;  

    print_r(json_encode($response));
?>
  • 2
    Possible duplicate of [Sending input data from android to php](http://stackoverflow.com/questions/21623455/sending-input-data-from-android-to-php) – LF00 Dec 31 '16 at 02:51

1 Answers1

0

Use Async task for faster send & retrieval to/fro server. Just create an object of the class and use execute function to pass parameters and start asynchronous sending and retrieval to/fro the server.

eg:- SendtoPhp stp = new SendtoPhp(); stp.execute(tname, phone)

public class SendtoPhp extends AsyncTask<String, Void, String> {


            @Override
            protected String doInBackground(String... params) {
                String tname = params[0]; //parameters you need to send to server
                String phone = params[1];


                String data = "";

                int tmp;
                try {

                    URL url = new URL("http://your.server.com/"); //url to php code
                    String urlParams = "tname=" + tname + "&tphone=" + phone;


                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    OutputStream os = httpURLConnection.getOutputStream();

                    os.write(urlParams.getBytes());
                    os.flush();
                    os.close();

                    InputStream is = httpURLConnection.getInputStream();


                    while ((tmp = is.read()) != -1) {


                        data += (char) tmp;

                        //System.out.println(data);
                    }

                    is.close();
                    httpURLConnection.disconnect();

                    return data;

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return "Exception: " + e.getMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                    return "Exception: " + e.getMessage();
                }
            }

            @Override
            protected void onPostExecute(String s) {
                try {
                    //You might receive something on server code execution using JSON object


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


            }
            }
Sumit Shetty
  • 112
  • 1
  • 9