2

I am having this problem with my insert in android with mysql and PHP. I'm looking in my logs to identify the error but then my application crashes after this log executes:

/LOGS: onResponse: response: Response{protocol=http/1.1, code=403, message=Forbidden, url=http://192.168.254.107/retrofit/insert.php}

which blocks me to connect with this URL

this is my insert.php

    <?php  

include "db.php";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $email = $_POST['email'];
    $user = $_POST['user'];
    $pass = $_POST['pass'];

    $response = array();

    $sql = "INSERT into users values(null,'$fname','$lname','$email','$user','$pass')";

    if ($con->query($sql)) {
        $response['success'] = 1;

        echo json_encode($response);
    }
    else{
        $response['success'] = 0;

        echo json_encode($response);
    }

    }

    ?>

And this would be my android file

Main.java

    package gd.rf.cracksoftware.retrofitregister;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class main extends Activity {


    private EditText inp_fname,inp_lname,inp_email,inp_username,inp_password;
    private static final String BASEURL = "http://192.168.254.107/";
    private final String TAG = "LOGS";

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

        inp_fname = findViewById(R.id.inp_fname);
        inp_lname = findViewById(R.id.inp_lname);
        inp_email = findViewById(R.id.inp_email);
        inp_username = findViewById(R.id.inp_user);
        inp_password = findViewById(R.id.inp_pass);
    }

    public void InsertClick(View view){
        String fname = inp_fname.getText().toString().trim();
        String lname = inp_lname.getText().toString().trim();
        String email = inp_email.getText().toString().trim();
        String user = inp_username.getText().toString().trim();
        String pass = inp_password.getText().toString().trim();

        Retrofit(fname,lname,email,user,pass);
    }

    private RestAPI GetInterfaceService(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RestAPI restAPI = retrofit.create(RestAPI.class);
        return restAPI;
    }
    private void Retrofit(final String fname, String lname, String email, final String user, String pass){

        RestAPI restAPI = this.GetInterfaceService();
        Call<user> userCall = restAPI.register(
            fname,lname,email,user,pass
        );
        userCall.enqueue(new Callback<gd.rf.cracksoftware.retrofitregister.user>() {
            @Override
            public void onResponse(Call<user> call, Response<user> response) {
                Log.d(TAG, "onResponse: firstname: "+fname);
                Log.d(TAG, "onResponse: calls: "+call.toString());
                Log.d(TAG, "onResponse: response: "+response);
                //user isSuccess = response.body();
                //String returnResponse = isSuccess.success;

                /*if(returnResponse.trim().equals("1")){

                    Toast.makeText(main.this, "Register Complete", Toast.LENGTH_SHORT).show();
                }
                else{

                    Toast.makeText(main.this, "Register Failed", Toast.LENGTH_SHORT).show();
                }*/
            }

            @Override
            public void onFailure(Call<user> call, Throwable t) {
                call.cancel();
                Toast.makeText(main.this, "Network failed to connect", Toast.LENGTH_SHORT).show();
            }
        });

    }
    }

my interface class RestAPI.java

package gd.rf.cracksoftware.retrofitregister;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface RestAPI {

@FormUrlEncoded
@POST("retrofit/insert.php")
Call<user> register(
        @Field("firstname") String first_name,
        @Field("lastname") String last_name,
        @Field("email") String email,
        @Field("user") String user,
        @Field("pass") String pass
);
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
James Tubiano
  • 231
  • 3
  • 17

2 Answers2

1

From the error code's documentation, we can see it is not an authentication problem, and that it's a server side error.

The HTTP 403 Forbidden client error status response code indicates that the server understood the request but refuses to authorize it.

This status is similar to 401, but in this case, re-authenticating will make no difference. The access is permanently forbidden and tied to the application logic (like an incorrect password).

So it is not a retrofit error. Try to add some logs in your php code.

Cecilia
  • 4,512
  • 3
  • 32
  • 75
andrei
  • 2,934
  • 2
  • 23
  • 36
  • 1
    i already did in the php file to POSTMAN it inserted but when i executed in the android it has the problem of response error 403 – James Tubiano Jun 04 '18 at 18:09
  • @JamesTubiano could you post the request done by retrofit? You can use Facebook's stetho library to view network calls – andrei Jun 04 '18 at 18:22
  • @JamesTubiano I have the same problem with a user using a Samsung Galaxy A53. Did you manage to find the reason? – Hmerman6006 May 04 '23 at 12:19
0

My code doesnt have the problem but my apache is because i ran it in the localhost but suddenly when i read the documentation about the request all granted in apache suddenly it works my bad by the way thanks guys more power stackoverflow

This might fix my problem:

How to enable local network users to access my WAMP sites?

James Tubiano
  • 231
  • 3
  • 17