0

I have been working on creating a login/register feature for an app I'm working on (NOTE: this app is for my own personal use right now, so I'm not looking to make my passwords super secure as of now). I have been trying to figure out why I am still getting this JSONException. I've used different username and password combos (the ones here are obviously examples), a bunch of different localhost/127.0.0.1/10.0.2.2 combinations, and nothing seems to be working. When testing it online, 10.0.2.2:8080/android_login_api/register.php says the page cannot be displayed. localhost:8080/android_login_api/register.php at least displays, but with errors saying there is an unknown host (being 10.0.2.2:8080). Also, I am using Port 80 with WAMP. While this post was said to be the same as org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject, HttpClient is outdated and I am unable to test that way. Any help would be greatly appreciated, thank you!

register.php:

<?php
$con = mysqli_connect("http://10.0.2.2:8080" , "EXAMPLE_USERNAME" , "EXAMPLE_PASSWORD" , "android_api");
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $name, $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;

echo json_encode($response);
?>

RegisterRequest.java:

package example.com.musicapptest;

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

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

/**
* Created by Carter Klein on 6/26/2016.
*/
public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://10.0.2.2:8080/android_login_api/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 example.com.musicapptest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 name = (EditText) findViewById(R.id.name);
        final EditText email = (EditText) findViewById(R.id.email);
        final EditText password = (EditText) findViewById(R.id.password);

        final Button registerButton = (Button) findViewById(R.id.btnRegister);
        final Button toLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String clickName = name.getText().toString();
                final String clickEmail = email.getText().toString();
                final String clickPassword = password.getText().toString();

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

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonResponse = new JSONArray(response);
                            boolean success = jsonResponse.getBoolean(Integer.parseInt("success"));
                            if (success) {
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(clickName, clickEmail, clickPassword, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}
Community
  • 1
  • 1
  • 1
    Your json looks invalid. What is the json string you are receiving from server? – Rohit5k2 Jul 01 '16 at 19:21
  • Thank you for a quick response! I'm still pretty new to app development and PHP/WAMP. Where can I see what JSON string I'm receiving from the server. When I got to localhost:8080/android_layout_api/register.php I don't see any type of JSON string, just a slew of errors about "No such host" and then subsequently how I have undefined indexes for name, email, password. And when I run the app in Android Studio the JSONException doesn't show any Strings, just the type of error and then all the instances it occurred. – Carter Klein Jul 01 '16 at 19:26
  • I'm no expert in php, but aren't you returning a JSONArray and then try to parse the string via JSONObject? – babadaba Jul 01 '16 at 19:28
  • To be honest, I'm not sure. I probably am returning a JSONArray. I changed JSONObject to JSONArray in RegisterActivity.java, and changed the following line to include `Integer.parseInt("success")` but that didn't work (it threw the same JSONException). Do you know how else I could go about this?? – Carter Klein Jul 01 '16 at 19:42
  • Try to log the response string with Log.e("TAG", response); and edit it in your question – babadaba Jul 01 '16 at 19:56
  • So it seems like the issue says that "No such host is known" on line 2 of my PHP, and that the index "email" on line 4 is undefined. Why would this be? Isn't http://10.0.2.2:8080 the correct way to access localhost port 8080 through Android? And I filled in my email when testing in the app, so I'm not sure why it's saying it's an undefined index. – Carter Klein Jul 01 '16 at 20:09
  • You will need to find out your local IP Adress of the hosting computer/server and use that ip adress. – babadaba Jul 01 '16 at 20:54
  • Last question I promise. Do I change this IP in both the Java and PHP files, or just the PHP? Also, do I still use the IP:8080, or just the IP? Thank you so much for your help by the way, you're awesome! – Carter Klein Jul 01 '16 at 20:57

0 Answers0