Java 在android改造中使用jwt令牌注册

Java 在android改造中使用jwt令牌注册,java,android,android-studio,jwt,retrofit2,Java,Android,Android Studio,Jwt,Retrofit2,我想用他们的令牌注册一个用户,我想用安卓系统中的改造注册一个用户,但我一直收到这个错误: ERROR::: Attempt to invoke virtual method 'void com.signup.User.setUsername(java.lang.String)' on a null object reference 这是我的密码: public class Session { Context context; private SharedPreferen

我想用他们的令牌注册一个用户,我想用安卓系统中的改造注册一个用户,但我一直收到这个错误:

ERROR::: Attempt to invoke virtual method 'void com.signup.User.setUsername(java.lang.String)' on a null object reference
这是我的密码:

    public class Session {
    Context context;
    private SharedPreferences prefs;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        this.context = cntx;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void setJwtToken(String token) {
        prefs.edit().putString("JwtToken", token).commit();
    }

    public String getJwtToken() {
        String token = prefs.getString("JwtToken", "");
        if (token == null || token.isEmpty()) {
            token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
        }
        return token;
    }

}

    public interface ApiInterface {

    @POST("/api/users/signup")
    Call<ResponseBody> signMeUp(@Header("Authorization") String token ,@Body User user);



}

    public class MainActivity extends AppCompatActivity {

    private EditText et_name, et_address, et_phone, et_username, et_email, et_password, et_confipassword;
    private Button register;
    private User user;
    private SharedPreferences prefs;
    private ApiInterface apiInterface;
    private Session session;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        et_name = findViewById(R.id.edit_text_name);
        et_address = findViewById(R.id.edit_text_address);
        et_phone = findViewById(R.id.edit_text_phonenumber);
        et_username = findViewById(R.id.edit_text_username);
        et_email = findViewById(R.id.edit_text_email);
        et_password = findViewById(R.id.edit_text_password);
        et_confipassword = findViewById(R.id.edit_text_confirm_password);


        register = findViewById(R.id.signupButton);
        register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
               Login();

            }

        });
    }


    private void Login() {


        user.setUsername(et_name.getText().toString());
        user.setAddress(et_address.getText().toString());
        user.setPhone(et_phone.getText().toString());
        user.setName(et_name.getText().toString());
        user.setEmail(et_email.getText().toString());
        user.setPassword(et_password.getText().toString());
        user.setPasswordConfirmation(et_confipassword.getText().toString());

        signupUser(user);
    }
    private void signupUser(final User user) {
        // Set up progressbar before call
        apiInterface = ApiClient.getClient().create(ApiInterface.class);

        Call<ResponseBody> call1 = apiInterface.signMeUp(session.getJwtToken(),user);

        final Gson gson = new Gson();
        final String json = gson.toJson(user);
        call1.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.code() == 201) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body().string());
                        //Starting main activity after user sees dialog
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else if (response.code() == 500) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.errorBody().string());
                        Log.e("SignupFragment", jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else
                    Log.e("SignupFragment", response.raw().toString());
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }


    }


    public class User {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("password")
    @Expose
    private String password;
    @SerializedName("password_confirmation")
    @Expose
    private String passwordConfirmation;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("phone")
    @Expose
    private String phone;
    @SerializedName("address")
    @Expose
    private String address;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("pan_no")
    @Expose
    private String panNo;
    @SerializedName("birthday")
    @Expose
    private String birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getPasswordConfirmation() {
        return passwordConfirmation;
    }

    public void setPasswordConfirmation(String passwordConfirmation) {
        this.passwordConfirmation = passwordConfirmation;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPanNo() {
        return panNo;
    }

    public void setPanNo(String panNo) {
        this.panNo = panNo;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }


}

    public class ApiClient {

    public static final String BASE_URL = "https://myapp.herokuapp.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }
    ApiInterface apiInterface=retrofit.create(ApiInterface.class);
}
公开课{
语境;
私人共享参考优先权;
公开会议(上下文cntx){
//TODO自动生成的构造函数存根
this.context=cntx;
prefs=PreferenceManager.GetDefaultSharedReferences(上下文);
}
公共无效setJwtToken(字符串令牌){
prefs.edit().putString(“JwtToken”,token.commit();
}
公共字符串getJwtToken(){
String-token=prefs.getString(“JwtToken”,”);
if(token==null | | token.isEmpty()){
token=“持票人Eyj0Exiaioijkv1qiljHbgcioijiuzi1nij9.Eyjlehaiojixnzc0nti3otksimlhdci6mtuxnjjjjjjjjjjk5oswiaxnzjjjjoiqfzb2jyxmgtmvwwwywwilcuymyioje1tywmj5otksimp0asic6Ikd1zxn0vg9rzw4ilcjdzdwijjb9.qikmngbymqch5hregf3x-zf3x-zffgggg5hjjjjjjjj;
}
返回令牌;
}
}
公共接口{
@POST(“/api/users/signup”)
调用signMeUp(@Header(“Authorization”)字符串令牌,@Body User);
}
公共类MainActivity扩展了AppCompatActivity{
私人编辑文本et_名称、et_地址、et_电话、et_用户名、et_电子邮件、et_密码、et_密码;
专用按钮寄存器;
私人用户;
私人共享参考优先权;
专用接口;
非公开会议;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=findviewbyd(R.id.edit_text_name);
et_地址=findViewById(R.id.edit_text_地址);
et_phone=findviewbyd(R.id.edit_text_phone number);
et_username=findviewbyd(R.id.edit_text_username);
et_email=findviewbyd(R.id.edit_text_email);
et_password=findviewbyd(R.id.edit_text_password);
et\u ConfipPassword=findViewById(R.id.edit\u text\u Confix\u password);
寄存器=findViewById(R.id.signupButton);
register.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
登录();
}
});
}
私有void登录(){
user.setUsername(et_name.getText().toString());
user.setAddress(et_address.getText().toString());
user.setPhone(et_phone.getText().toString());
user.setName(et_name.getText().toString());
user.setEmail(et_email.getText().toString());
user.setPassword(et_password.getText().toString());
user.setPasswordConfirmation(et_confippassword.getText().toString());
注册用户(用户);
}
私有无效注册用户(最终用户){
//在调用前设置progressbar
apinterface=ApiClient.getClient().create(apinterface.class);
调用call1=apinterface.signMeUp(session.getJwtToken(),user);
最终Gson Gson=新Gson();
最终字符串json=gson.toJson(用户);
call1.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.code()=201){
试一试{
JSONObject=newJSONObject(response.body().string());
//在用户看到对话框后启动主活动
}捕获(JSONException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}else if(response.code()==500){
试一试{
JSONObject=newJSONObject(response.errorBody().string());
e(“SignupFragment”,jsonObject.toString());
}捕获(JSONException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}否则
Log.e(“SignupFragment”,response.raw().toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
}
公共类用户{
@序列化名称(“名称”)
@暴露
私有字符串名称;
@序列化名称(“电子邮件”)
@暴露
私人字符串电子邮件;
@序列化名称(“密码”)
@暴露
私有字符串密码;
@SerializedName(“密码确认”)
@暴露
私有字符串密码确认;
@序列化名称(“图像”)
@暴露
私有字符串图像;
@序列化名称(“电话”)
@暴露
私人电话;
@序列化名称(“地址”)
@暴露
私有字符串地址;
@序列化名称(“用户名”)
@暴露
私有字符串用户名;
@序列化名称(“盘号”)
@暴露
私有字符串panNo;
@序列化名称(“生日”)
@暴露
私人串生日;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getPasswordConfirmation(){
返回密码确认;
}
public void setPasswordConfirmation(字符串passwordConfirmation){
this.passwordConfirmation=passwordConfirmation;
}
公共字符串getImage(){
返回图像;
}
公共void setImage(字符串图像){
这个图像=图像;
}
公共字符串getPhone(){
回电话;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        user = new User();
        session = new Session(this);
}