有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android改型v2插入失败错误403

我在安卓中使用mysql和PHP时遇到了这个问题。我正在查看日志以确定错误,但执行此日志后,我的应用程序崩溃:

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

这会阻止我连接此URL

这是我的插入。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);
    }

    }

    ?>

这将是我的安卓文件

Main。java

    package gd.rf.cracksoftware.retrofitregister;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.EditText;
import 安卓.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();
            }
        });

    }
    }

我的接口类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
);
}

共 (2) 个答案

  1. # 1 楼答案

    从错误代码的documentation中,我们可以看出这不是身份验证问题,而是服务器端错误

    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).

    所以这不是改装错误。尝试在php代码中添加一些日志

  2. # 2 楼答案

    我的代码没有问题,但我的apache是因为我在本地主机上运行了它,但突然,当我阅读有关请求的文档时,所有请求都是在apache中授予的。顺便说一句,这对我来说很糟糕,谢谢大家,更多的能力

    这可能会解决我的问题:

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