gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke response. body.getMessage()方法改造错误

转载 作者:行者123 更新时间:2023-12-02 09:46:35 25 4
gpt4 key购买 nike

我是改造新手。每当我尝试使用 Android 手机注册时,都会出现错误:java.lang.NullPointerException: Attempt to invoke response.body.getMessage()response.body.getError() .

它可以在本地主机中运行,但不能在实时服务器中运行。我也可以登录,但无法注册。我正在关注本教程=https://www.simplifiedcoding.net/retrofit-android-tutorial/

请帮助我!

这是我收到错误的代码:

            call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
//hiding progress dialog
progressDialog.dismiss();

Toast.makeText(getApplicationContext(), response.body().getMessage(), Toast.LENGTH_LONG).show();

//if there is no error
if (!response.body().getError()) {
//starting profile activity
finish();
SharedPrefManager.getInstance(getApplicationContext()).userLogin(response.body().getUser());
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}
}

这里是Result.java

public class Result {
@SerializedName("error")
private boolean error;

@SerializedName("message")
private String message;

@SerializedName("user")
private User user;

public Result(boolean error, String message, User user) {
this.error = error;
this.message = message;
this.user = user;
}

public boolean getError() {
return error;
}

public String getMessage() {
return message;
}

public User getUser() {
return user;
}

谢谢! code screen

这是我的index.php 文件

    $app->post('/register', function (Request $request, Response $response) {
if (isTheseParametersAvailable(array('name', 'email', 'password', 'address', 'phone'))) {
$requestData = $request->getParsedBody();
$name = $requestData['name'];
$email = $requestData['email'];
$password = $requestData['password'];
$address = $requestData['address'];
$phone = $requestData['phone'];
$db = new DbOperation();
$responseData = array();

$result = $db->registerUser($name, $email, $password, $address, $phone);

if ($result == USER_CREATED) {
$responseData['error'] = false;
$responseData['message'] = 'Registered successfully';
$responseData['user'] = $db->getUserByEmail($email);
} elseif ($result == USER_CREATION_FAILED) {
$responseData['error'] = true;
$responseData['message'] = 'Some error occurred';
} elseif ($result == USER_EXIST) {
$responseData['error'] = true;
$responseData['message'] = 'This email already exist, please login';
}

$response->getBody()->write(json_encode($responseData));
}

});

这里是User.java

    public class User {

private int id;
private String name;
private String email;
private String password;
private String address;
private String phone;

public User(String name, String email, String password, String address, String phone) {
this.name = name;
this.email = email;
this.password = password;
this.address = address;
this.phone = phone;
}

public User(int id, String name, String email, String address, String phone){
this.id = id;
this.name = name;
this.email = email;
this.address = address;
this.phone = phone;
}

public User(int id, String name, String email, String password, String address, String phone) {
this.id = id;
this.name = name;
this.email = email;
this.password = password;
this.address = address;
this.phone = phone;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public String getPassword(){
return password;
}

public String getAddress() {
return address;
}

public String getPhone() {
return phone;
}

}

DbOperation.php用于注册用户的函数

     //Method to create a new user
function registerUser($name, $email, $pass, $address, $phone)
{
if (!$this->isUserExist($email)) {
$password = md5($pass);
$stmt = $this->con->prepare("INSERT INTO users (name, email, password, address, phone) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $name, $email, $password, $address, $phone);
if ($stmt->execute())
return USER_CREATED;
return USER_CREATION_FAILED;
}
return USER_EXIST;
}

完整的注册java代码:

    public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonSignUp;
private EditText editTextName, editTextEmail, editTextPassword, editTextAddress, editTextPhone;
public static final String TAG = "ins";

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

buttonSignUp = (Button) findViewById(R.id.buttonSignUp);

editTextName = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextAddress = (EditText) findViewById(R.id.editTextAddress);
editTextPhone = (EditText) findViewById(R.id.editTextPhone);

buttonSignUp.setOnClickListener(this);
}

private void userSignUp() {

//defining a progress dialog to show while signing up
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Signing Up...");
progressDialog.show();

String name = editTextName.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();


//building retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();


//Defining retrofit api service
APIService service = retrofit.create(APIService.class);

//Defining the user object as we need to pass it with the call
User user = new User(name, email, password, address, phone);

//defining the call
Call<Result> call = service.createUser(
user.getName(),
user.getEmail(),
user.getPassword(),
user.getAddress(),
user.getPhone()
);

//calling the api
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
//hiding progress dialog
progressDialog.dismiss();

Toast.makeText(getApplicationContext(), response.body().getMessage(), Toast.LENGTH_LONG).show();

//if there is no error
if (!response.body().getError()) {
//starting profile activity
finish();
SharedPrefManager.getInstance(getApplicationContext()).userLogin(response.body().getUser());
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}
}

@Override
public void onFailure(Call<Result> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

@Override
public void onClick(View view) {
if (view == buttonSignUp) {
userSignUp();
}
}

端点

     @Headers("Content-Type: application/json")
//The register call
@FormUrlEncoded
@POST("/service/people/register")
Call<Result> registerUser(
@Field("name") String name,
@Field("email") String email,
@Field("password") String password,
@Field("address") String address,
@Field("phone") String phone);

最佳答案

当我从 php Web 服务器收到响应时,我遇到了同样的问题。就我而言,服务器响应不正确。服务器响应类型必须等于 Android 之一。

在您的例子中,有 3 个成员 - Boolean 错误、String 消息和 User 用户。也许,User 类格式似乎与服务器响应不相等。

//////请尝试这个/////

    public void onResponse(Call<Result> call, Response<Result> response) {
//hiding progress dialog
progressDialog.dismiss();

//if there is no error
if (response.isSuccessful()) {
Toast.makeText(getApplicationContext(), response.body().getMessage(), Toast.LENGTH_LONG).show();


//starting profile activity
finish();
SharedPrefManager.getInstance(getApplicationContext()).userLogin(response.body().getUser());
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}
}

关于java.lang.NullPointerException : Attempt to invoke response. body.getMessage()方法改造错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com