1

I need to create an Android app with a register activity, so I need to insert some data into a mysql database. I tried some different tutorials and guides but nothing helped me to resolve my problem.

Who can help me?

I post my last code here:

Register.java

public class Register extends AppCompatActivity implements View.OnClickListener {

private Button bRegister;
private EditText etName, etSurname, etAge, etEmail, etPassword;

private static final String REGISTER_URL = "http://localhost/sFitness/Register.php";

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

    etName = (EditText) findViewById(R.id.etName);
    etSurname = (EditText) findViewById(R.id.etSurname);
    etAge = (EditText) findViewById(R.id.etAge);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPassword = (EditText) findViewById(R.id.etPassword);

    bRegister = (Button) findViewById(R.id.bRegister);
    bRegister.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bRegister:
            registerUser();
            break;
    }
}

private void registerUser() {
    String name = etName.getText().toString();
    String surname = etSurname.getText().toString();
    String age = etAge.getText().toString();
    String email = etEmail.getText().toString();
    String password = etPassword.getText().toString();

    register(name, surname, age, email, password);
}

private void register(String name, String surname, String age, String email, String password) {
    class RegisterUser extends AsyncTask<String, Void, String> {

        ProgressDialog loading;
        RegisterUserClass ruc = new RegisterUserClass();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            loading = ProgressDialog.show(Register.this, "Please wait...", null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {
            HashMap<String, String> data = new HashMap<String, String>();
            data.put("name", params[0]);
            data.put("surname", params[1]);
            data.put("age", params[2]);
            data.put("email", params[3]);
            data.put("password", params[4]);

            String result = ruc.sendPostRequest(REGISTER_URL, data);
            return result;
        }
    }
    RegisterUser ru = new RegisterUser();
    ru.execute(name, surname, age, email, password);
}
}

RegisterUserClass.java

    import org.apache.http.params.HttpParams;
import org.apache.http.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by Silvia Berardo on 29/12/2015.
 */



public class RegisterUserClass {

public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            response = br.readLine();
        }
        else {
            response="Error Registering";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}
}

The problem is that this code send me a message that tell me the database doesn't work... why?

bsiilvia
  • 97
  • 1
  • 10
  • What is the error message you get back? – apmartin1991 Dec 30 '15 at 09:53
  • When I run the application, after the click on the register button, the message with the successful or the failed was not showed and into the database there isn't nothing. Into console: `E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4053810` `W/System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)` `E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4057860` `E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40578d0` @apmartin1991 – bsiilvia Dec 30 '15 at 10:09
  • I would assume you get this error on your android simulator yes? – apmartin1991 Dec 30 '15 at 10:25
  • yes @apmartin1991 do you know the solution? – bsiilvia Dec 30 '15 at 10:27

1 Answers1

0

replace line:

private static final String REGISTER_URL = "http://localhost/sFitness/Register.php";

With

private static final String REGISTER_URL = `"http://10.0.2.2/sFitness/Register.php";`

localhost does not refer to your PCs IP address but it refers to the localhost on your simulator. If you use 10.0.2.2 then it should work or bring up another error message.

if you are using GenyMotion then you need to use:

private static final String REGISTER_URL = `"http://10.0.3.2/sFitness/Register.php";`

All of this information was found here

Community
  • 1
  • 1
apmartin1991
  • 3,064
  • 1
  • 23
  • 44