0

I'm trying to connect to a server provided for me by another developer in the team. I'm using Volley, still a beginner though.

On the server there is a register method inside the user folder which is inside the API folder. I need to be able to call this method from my android app in order to signup a new user.

Here is the code I'm using.

try {

    Request request = new Request(Request.Method.POST, url + "/user/register", new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                protected Response parseNetworkResponse(NetworkResponse response) {
                    Toast.makeText(getApplicationContext(), "ParseNetworkRequest", Toast.LENGTH_LONG).show();
                    return null;
                }
            @Override
            protected void deliverResponse(Object response) {
                Toast.makeText(getApplicationContext(), "DeliverResponse", Toast.LENGTH_LONG).show();
            }

            @Override
            public int compareTo(Object another) {
                return 0;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                // POST Parameters
                params.put("api_key", API_KEY);
                params.put("email", etEmail.getText().toString());
                params.put("password", etPassword.getText().toString());
                params.put("verifyPassword", etConfirmPass.getText().toString());
                params.put("firstname", etFirstName.getText().toString());
                params.put("lastname", etLastName.getText().toString());
                params.put("birthday", birthDate);
                return params;
            }
        };

        Volley.newRequestQueue(this).add(request);

The error I'm getting.

com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname

The permissions in my manifest.xml since most of the questions asked before were solved by adding this lines, didn't work in my situation.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE" />

Note that I tried to use the RequestString class and I got the same error, but when I tried to use another url ("http://google.com") I got a server error, which is a bit nicer because we know why.

Motassem Jalal
  • 1,254
  • 1
  • 22
  • 46
  • Have you turned on your wifi connection? Moreover, try access the Url from web browser of the phone to make sure it is accessible – BNK Sep 19 '15 at 14:34
  • @BNK Yes sure...tried that also, it is accessible. – Motassem Jalal Sep 19 '15 at 14:35
  • Moreover, try access the Url from web browser of the phone to make sure it is accessible – BNK Sep 19 '15 at 14:36
  • Please post the code that initializing `url ` variable – BNK Sep 19 '15 at 14:37
  • @BNK I'm sorry, i'm not allowed to do that, but it is something like this: "http://test.php.com/api" – Motassem Jalal Sep 19 '15 at 14:41
  • I have same issue that time i missed "http://" in that – N J Sep 19 '15 at 14:44
  • @Nilesh I have "http://" in the url – Motassem Jalal Sep 19 '15 at 14:44
  • Ok, try using IP instead hostname – BNK Sep 19 '15 at 14:45
  • Put in a log statement that prints the full url, just to check it uses the url you think it's using. Then, use restclient or a similar tool to access the api. – Christine Sep 19 '15 at 14:46
  • @Christine the url is the same, I checked it while debugging. – Motassem Jalal Sep 19 '15 at 14:58
  • Have you tried using IP address instead of hostname yet? I guess because of using hostname, the phone cannot find that hostname until you set DNS server in your phone – BNK Sep 19 '15 at 15:10
  • Are you running the app on a device? Maybe the device is on another network as your computer and the host is reachable from one network but not from the other? I use a host with a local IP number that is reachable from my phone when it's on wifi, but not if it's on 4G. – Christine Sep 19 '15 at 15:12
  • @Christine: if using 3G/4G, your host IP must be accessible/published in the Internet – BNK Sep 19 '15 at 15:16
  • If my server is on 192.168.88.249, it's accessible from my phone when it's on wifi, but not when it's on the Vodafone network. – Christine Sep 19 '15 at 15:18
  • @Christine: that because the IP is only accessible in your LAN/WAN. If via mobile providers's networks, the IP must be published (means that from any device in the Internet can access/ping that published IP) – BNK Sep 19 '15 at 15:24
  • @BNK Not yet, I'm gonna try that. – Motassem Jalal Sep 19 '15 at 16:51
  • @Christine The server is not on my network, it is actually not even in my country. – Motassem Jalal Sep 19 '15 at 16:52
  • 1
    Well, there must be an error in the url you provide, or the server is not there. – Christine Sep 19 '15 at 20:31
  • @Christine I chicjed the solution [here](http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working/19945676#19945676). I got a successful call for one time only. I added a new user to the server, but afterwards I wasn't able to do that again! – Motassem Jalal Sep 20 '15 at 12:54

1 Answers1

0

Using a different kind of connection solved the problem, I tried to run the app using 3G and it worked.

Motassem Jalal
  • 1,254
  • 1
  • 22
  • 46