在 android studio 中使用 retrofit lib 时出现错误 400 的原因是什么?

What is the reason of getting error 400 when you want to use retrofit lib in android studio?

在下面的代码中,我试图从 RegisterFragment 中的用户获取 3 个字符串(用户名、密码和电子邮件),并通过 android 中的 Retrofit 库将其传递给服务器] 工作室。但是我得到 Error 400。我知道它应该可以工作,因为这是我们的作业之一,其他人也成功地完成了,但我不知道我的代码的哪一部分是错误的!

我尝试调试程序,在 RegisterController.java 内部的 onFailure 函数中它说:

onFailure():= java.lang.illegalArgumentException: invalid argument count. expected 2 , received 0

MainActivity.java:

package com.m.chatroom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.m.chatroom.Data.ChatRoomAPI;
import com.m.chatroom.Data.RegisterserController;
import com.m.chatroom.Models.User;

public class MainActivity extends AppCompatActivity {

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

        RegisterFragment registerFragment = new RegisterFragment();
        getFragmentManager().beginTransaction().add(R.id.container_main , registerFragment)
                .addToBackStack(null).commit();


    }

}

聊天室API :

package com.m.chatroom.Data;


import com.m.chatroom.Models.User;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface ChatRoomAPI {

    public static final String auth= "5a1d*******fabd";
    String BASE_URL = "https://api.backtory.com/";

    @Headers("X-Backtory-Authentication-Id:5a1d4b******fabd")
    @POST("auth/users")
    Call<User> registerUser(@Body User user);

    interface RegisterUserCallback
    {
        void onResponse(User user);
        void onFailure(String cause);
    }
}

注册控制器:

package com.m.chatroom.Data;


import android.util.Log;

import com.m.chatroom.Data.ChatRoomAPI;
import com.m.chatroom.Models.User;

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

public class RegisterserController {

    ChatRoomAPI.RegisterUserCallback registerUserCallback;

    public RegisterserController(ChatRoomAPI.RegisterUserCallback registerUserCallback) {
        this.registerUserCallback = registerUserCallback;
    }

    public void start(User user)
    {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ChatRoomAPI.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();

        ChatRoomAPI chatRoomAPI = retrofit.create(ChatRoomAPI.class);
        Call<User> call = chatRoomAPI.registerUser(user);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                Log.wtf("TAG" , "onresposne  " + response.code() );
                registerUserCallback.onResponse(response.body());


            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

                Log.wtf("TAG" , "failure  " + t.getCause() );
                registerUserCallback.onFailure(t.getCause().getMessage());


            }
        });

    }
}

注册片段:

package com.m.chatroom;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.m.chatroom.Data.ChatRoomAPI;
import com.m.chatroom.Data.RegisterserController;
import com.m.chatroom.Models.User;


public class RegisterFragment extends android.app.Fragment {

    private EditText userName;
    private EditText password;
    private EditText email;
    private Button registerButton;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_register, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        userName = (EditText) view.findViewById(R.id.editText_user);
        password = (EditText) view.findViewById(R.id.editText_pass);
        email = (EditText) view.findViewById(R.id.editText_email);
        registerButton = (Button) view.findViewById(R.id.button_register);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                RegisterserController registerserController = new RegisterserController(registerUserCallback);
                User user = new User();

                user.setPassword(password.getText().toString());
                user.setEmail(email.getText().toString());
                user.setUserName(userName.getText().toString());
                registerserController.start(user);

            }
        });

    }
    ChatRoomAPI.RegisterUserCallback registerUserCallback = new ChatRoomAPI.RegisterUserCallback() {
        @Override
        public void onResponse(User user) {
            Toast.makeText(getActivity(), user.getUserName() , Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onFailure(String cause) {
            Toast.makeText(getActivity(), cause , Toast.LENGTH_SHORT).show();


        }
    };
}

User.java:

package com.m.chatroom.Models;



public class User {

    private String userName;
    private String password;
    private String email;
    private String phoneNumber;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String serName) {
        this.userName = serName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public User() {


    }
}

编辑:在邮递员软件中,我可以发送这个查询:

{
    "username":"xyzxyzxyz",
    "password":"xyzxyzxyz",
    "email":"xyzxyzxyz@gmail.com"
}

得到这个结果(代码 201):

{
    "firstName": null,
    "lastName": null,
    "instanceId": "5a1d*****16474fabd",
    "phoneNumber": null,
    "active": "true",
    "guest": "false",
    "avatar": null,
    "creationDate": "2018-02-09T06:35:04.561Z",
    "userId": "5a7d4118e4b02316b5e5fe09",
    "email": "xyzxyzxyz@gmail.com",
    "username": "xyzxyzxyz"
}

也许这很愚蠢,但我在 User.java 中将这行代码 private String userName; 更改为 private String username; 并且成功了!

如您所见

{
    "username":"xyzxyzxyz",
    "password":"xyzxyzxyz",
    "email":"xyzxyzxyz@gmail.com"
}

API 得到 username 而不是 userName !