我在使用 mysql 和 PHP 插入 android 时遇到了这个问题。我正在查看我的日志以确定错误,但在执行此日志后我的应用程序崩溃了:
/LOGS: onResponse: response: Response{protocol=http/1.1, code=403, message=Forbidden, url=http://192.168.254.107/retrofit/insert.php}
哪个阻止我连接到这个 URL
这是我的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);
}
}
?>
这将是我的 android 文件
主.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();
}
});
}
}
我的接口(interface)类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
);
}
最佳答案
来自错误代码的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 代码中添加一些日志。
关于java - Android retrofit v2 插入失败错误403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686120/