0

Fixed it, please see the solution at the bottom

First of all sorry if this is a duplicate, trust me I tried searching if there was an existing answer...

OK so long story short I was following this tutorial (https://www.youtube.com/watch?v=wKwCgabRV2A). I had downloaded Wamp64 and set it up etc, set up my phpMyAdmin database and created the relevant table etc. however I didn't do anything more than set it up and run it.

I am attempting to use the following code inside of Android Studio to add the user's token ID to the database as follows:

    final String token = FirebaseInstanceId.getInstance().getToken();
    Log.d("TAG", token); //Just to confirm the token.

    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.REGISTRATION_SCRIPT_ADDRESS,
            new Response.Listener<String>(){
                  @Override
                  public void onResponse(String response){

                       }
                  },
            new Response.ErrorListener() {
                  @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                  })
                  {
                     @Override
                     protected Map<String, String> getParams() throws AuthFailureError {
                          Map<String, String> params = new HashMap<>();
                          params.put("user_token", token);

                          return params;
                    }
             };

VolleyRequestManager.getManagerInstance(SplashActivity.this).addRequestToQueue(stringRequest);

The PHP file address is in the Config class:

public static final String REGISTRATION_SCRIPT_ADDRESS = "http://192.168.1.106/bbservice/server_token_insert.php";

However it doesn't work, and I simply get the following:

08-17 21:55:45.089 4206-4352/com.example.program E/Volley: [2745] BasicNetwork.performRequest: Unexpected response code 403 for http://192.168.1.106/bbservice/server_token_insert.php
08-17 21:55:45.102 4206-4352/com.example.program E/Volley: [2745] BasicNetwork.performRequest: Unexpected response code 403 for http://192.168.1.106/bbservice/server_token_insert.php
08-17 21:55:45.181 4206-4206/com.example.program W/System.err: com.android.volley.AuthFailureError
08-17 21:55:45.181 4206-4206/com.example.program W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:159)
08-17 21:55:45.181 4206-4206/com.example.program W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

With no changes made to the table at all.

Basically how can I solve the Code 403 problem? Is it something to do with my network permissions (I'm running the Wamp server on the same PC that I'm running Android Studio on)? If so what permissions do I need to change? Or is it a code problem?

Note: I am attempting to run this on my actual android device, not an emulator.

Any help is GREATLY appreciated!

Edit 1: The PHP script I call here is as follows:

<?php

require "server_connector.php";

$user_token = $_POST["user_token"];
$query = "INSERT INTO tokens VALUES('".$user_token."');";

mysqli_query($connection, $query);
mysqli_close($connection);

?>

Server Connector:

<?php

$host = "localhost";
$user = "root";
$password = "";
$database = "bbservice";

$connection = mysqli_connect($host, $user, $password, $database);

if($connection){
    echo "Connection Established";
}else{
    echo "Connection Failed";
}

?>

These are both located within the C://Wamp64/www/BBService/ folder.

Edit 2:

Ok so now I'm getting still getting the same error after reinstalling wamp... I checked and confirmed the wamp apache service is the only service listening to Port 80 on my computer.

I have also attempted disabling Windows Firewall and it still did not work...

Edit 3: I tried another method to get this to work using OkHttp, using this method:

OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder().add("user_token", token).build();
Request request = new Request.Builder().url(Config.REGISTRATION_SCRIPT_ADDRESS).post(body).build();

try {
     Response response = client.newCall(request).execute();
     Log.d("SPLASH", "Executed request successfully.");
     Log.d("SPLASH", "Response: " + response.body().string());

}catch(IOException ioex){
     ioex.printStackTrace();
}

Aaaand I get the following:

Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /bbservice/server_token_insert.php
this server.<br />
</p>
<address>Apache/2.4.18 (Win64) PHP/5.6.19 Server at 192.168.1.108 Port 80</address>
</body></html>

If I look at the wamp server information at localhost, it clearly states:

Apache/2.4.18 (Win64) PHP/5.6.19 - Port defined for Apache: 80 This error occurs with both Windows Firewall on and off...I may start ripping out hair soon...

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
SchoolJava101
  • 609
  • 1
  • 5
  • 8
  • 1
    extreme nitpick: you **DO NOT** have a 'phpmyadmin' table. You have a table in the mysql database, for which phpmyadmin is an ADMINISTRATIVE INTERFACE. And if you're getting a 403, that has absolutely nothing to do with mysql in the first place. Your http request isn't even reaching your server-side database-handling script in the first place. – Marc B Aug 17 '16 at 20:08
  • Ok thanks for clarifying, I fixed up that technicality now, and cleared up the question slightly (hopefully) – SchoolJava101 Aug 17 '16 at 20:14
  • @SchoolJava101 How you are trying to send your data to wamp ? You need scripting language to send your data to the server like php or javascript you can use. Where is your php or the javascript code ?? – Lokesh Pandey Aug 17 '16 at 20:17
  • @SchoolJava101 that I understood you need one script to send your data to the wamp – Lokesh Pandey Aug 17 '16 at 20:22
  • @Lokesh I edited the question to include the scripts. – SchoolJava101 Aug 17 '16 at 20:23
  • @SchoolJava101 In your Java file where did you assign the link to your wamp server ?? – Lokesh Pandey Aug 17 '16 at 20:29
  • @Lokesh also not sure if it's worth mentioning I get an error stating that the 'user_token' in the first PHP file was undefined when I click on the link in the logcat, but I'm not sure if that's just a result of the first error as the information couldn't send... – SchoolJava101 Aug 17 '16 at 20:29
  • @Lokesh Edited the post again, the String location is in a general file (Config). (See above for the actual string) – SchoolJava101 Aug 17 '16 at 20:31
  • @SchoolJava101 If it's localhost by default link is 127.0.0.1 – Lokesh Pandey Aug 17 '16 at 20:33
  • And also make sure you port number is correct – Lokesh Pandey Aug 17 '16 at 20:37
  • @Lokesh the location is "http://192.168.1.106/bbservice/server_token_insert.php". When I tried using localhost it gave me an error stating it couldn't find localhost/127.0.0.1:80 – SchoolJava101 Aug 17 '16 at 20:39
  • What error ? Then there is a problem with your wamp server. Check you wamp server again or reinstall it – Lokesh Pandey Aug 17 '16 at 20:44
  • @Lokesh I uninstalled and reinstalled wamp, still the same error as above. When I use localhost it states it can't connect to 127.0.0.1. (Port 80) and a bunch of other stuff. – SchoolJava101 Aug 17 '16 at 20:53

2 Answers2

1

Ok so I finally managed to fix it. Firstly I completely reinstalled Wamp. Then I read up that you need to add specific IP addresses/groups to allow access to your folders in the

\wamp\bin\apache\apache{version}\conf\extra\httpd-vhosts.conf

file, thanks to this. Now everything works perfectly. Thanks for trying to help @Lokesh!

Community
  • 1
  • 1
SchoolJava101
  • 609
  • 1
  • 5
  • 8
0

@SchoolJava101 With above discussion there can be many possibilities that why you are unable to connect it to your wamp server.

Make sure no other program conflict Wamp such as IIS, Microsoft Web Deploy, SQL, Skype, Zonealarm, firewall/antivirus, NOD32, Eset, any web related program including Remote Desktop, Teamviewer or Apache, MySQL, PHP outside wamp folder (ie. in Program Files or System32 folder) ...

IIS and Apache/Wamp are both web server and might conflict in some way, so you have to disable IIS in order for Wamp to work

Disable IIS in Vista/W7:

Control Panel, Uninstall Programs, Turn Widows Features On or Off, uncheck Internet Information Services

Disable IIS in XP:

Control Panel, Add/Remove Programs, Add/Remove Windows Components, uncheck Internet Information Services (IIS)

Restart computer, then restart Wamp

Also, in folder C:\WINDOWS\System32\drivers\etc, open file hosts and delete anything in this file and have only this line below and nothing else

127.0.0.1 localhost

If using Skype, open Skype > Tools > Options > Advanced > Connection then uncheck "use port 80..." restart Wamp then Skype

Lokesh Pandey
  • 1,739
  • 23
  • 50