0

I'm trying to perform a POST request using Volley, but I keep getting the error com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "<my custom API URL>". No address associated with hostname. The request seems to work using Postman and I have already gave the necessary permissions in the Manifest (both INTERNET and ACCESS_NETWORK_STATE). I'm using a local server with SequelPro and Laravel. Here is the Postman working screens: Postman body Postman headers

Here is the part of the code of the Login Activity:

public class LoginActivity extends AppCompatActivity {

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

        final EditText emailInput = findViewById(R.id.emailInput);
        final EditText passwordInput = findViewById(R.id.passwordInput);
        final EditText confirmPasswordInput = findViewById(R.id.confirmPasswordInput);
        final Button loginBtn = findViewById(R.id.loginBtn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String email = emailInput.getText().toString();
                final String password = passwordInput.getText().toString();
                final String confirmPassword = confirmPasswordInput.getText().toString();

                try {
                    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
                    String URL = "http://lalalaravel.test/api/login";
                    JSONObject jsonBody = new JSONObject();

                    jsonBody.put("email", email);
                    jsonBody.put("password", password);
                    jsonBody.put("password_confirmation", confirmPassword);

                    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d("VOLLEY", response.toString());
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("VOLLEY", error.toString());
                        }

                    }) {
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            final Map<String, String> headers = new HashMap<>();
                            headers.put("Authorization", "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjM4MmM1OTc5YzFhN2Y1ZDczMjFjMWQwMThhYzBmMTdkZWI1MzlkMjIxN2M2YWRhMDUxNzhlMTcyNmVmOWQwZDM3ZGY2ZTdlNDIxOGUxNmQ5In0.eyJhdWQiOiIxIiwianRpIjoiMzgyYzU5NzljMWE3ZjVkNzMyMWMxZDAxOGFjMGYxN2RlYjUzOWQyMjE3YzZhZGEwNTE3OGUxNzI2ZWY5ZDBkMzdkZjZlN2U0MjE4ZTE2ZDkiLCJpYXQiOjE1MjkyNTg2OTAsIm5iZiI6MTUyOTI1ODY5MCwiZXhwIjoxNTYwNzk0NjkwLCJzdWIiOiIyMSIsInNjb3BlcyI6W119.hyjXMXwRb3GLVJ7w5KBmzwVDzoPcztjXKHOx8fqwN6jOvTfgki_HngAIB3JaG5Xhb7w8mFBX03fUDwzOc2S108SiBTjZ7B14GtYmlC8OgxVkKXcCqJ_0UOZBsgCYo7p7f4HBIgIyZ8StrsNlNbuj_4W5Pz2YY4WitbvDX9LsZFIbdz3vhVlmtR2zKA23hyRGUsWZniB7zLGEvsUt5XA9NNAy9XWySaw7JuRZ4uyYm0bXHrJKHSTmfBd9KOJSdpnWs_9miNvh5g_DJo39L7awAlG7-cb67Ih00bhrmgGAWp6VCEd5h-EhIc-l1qfYT56osDgnYIqMk3cAgIWrYrhrs6-mZ1miXqipk9dClliUO7DBylZW8cRbQ5PQ5xisE_wCMTSUr7VYE_7RyjMzDaZuctf8kHQ_W27ED9tq14NhgvJs4hkpO6Gzr32zyMj-KRan2Fc3O1CYgyJx0OR2LhvXrxp2PxfUSIzR8eGHOGypLicU_suG3Dsz_UG7Kp5EdhP8Ma9eDyWObfU64iFYZsuz58TOBOH2ssH7_PpKHBGmchxQSjpKsIwsjeYpdbVmzXG7cZe2_AOEZpXmlwkaAZLow3_kDPfvuoCIWVsEKfgRs72t3B8fIwSt-vWaXpGYKjGOjf9ZOXpiKjG1XHaur0vLR1eiIXKVFSyn6y42FIlbSqk");
                            headers.put("Content-Type", "application/json");
                            return headers;
                        }
                    };

                    queue.add(jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Honestly I don't know what to do, Internet is working fine and some of my classmates are doing their requests with iOs with no problem, it shouldn't be a connection problem...

  • Is your device and server on the same network? Whats the ip address of both? – petey Jun 29 '18 at 17:30
  • @petey Yes, I'm running the app on the Android Studio emulator and the server is on the same computer. – Zorokiller Jun 29 '18 at 17:34
  • Use an ipaddress instead of that host name. – greenapps Jun 29 '18 at 17:35
  • 1
    Then use 10.0.2.2 as ip address. – greenapps Jun 29 '18 at 17:35
  • @greenapps is correct, please see https://stackoverflow.com/a/6310592/794088 – petey Jun 29 '18 at 17:41
  • @greenapps I get the error: [768] BasicNetwork.performRequest: Unexpected response code 404 for http://10.0.2.2/api/login – Zorokiller Jun 29 '18 at 18:09
  • Where do you get that? Try to be informative please. – greenapps Jun 30 '18 at 05:32
  • Yes, sorry. The above message is the erro given from the function onErrorResponse of Volley. However, after more investigation I discovered that the IP addresses of emulated device and local server are different: SequelPro connects to localhost, while the emulator is using a standard Android IP address (192.168.232.2). My computer is connected to my wifi, which has a third different IP. Doing the traceroute of the hostname gives me that its IP is actually localhost. The main problem is that I'm still terrible with all these concepts... – Zorokiller Jun 30 '18 at 07:21

0 Answers0