我是Retrofit图书馆的新手。当我在我的移动应用程序调试时看到下面的“注册活动”时,单击“注册”按钮
后活动就会消失 public class RetrofitFishFarmApi {
private static final String AUTHORIZATION = "Authorization";
// main client
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static final String BASE_API_URL = "http://192.168.43.1:3000/";
// Retrofit
private static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_API_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null);
}
public static <S> S createService(Class<S> serviceClass,
final String authToken) {
// IMP: always clear the interceptors.
if (authToken != null && !authToken.equals("")) {
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header(AUTHORIZATION, authToken)
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
}
// build the Retrofit instance with the Token Authorization OkHttpClient.
Retrofit retrofit = builder.client(httpClient.build()).build();
// return the ServiceClass passed.
return retrofit.create(serviceClass);
}
注册活动如下
public class RegisterActivity extends AppCompatActivity {
private Button txtRegister;
private TextInputLayout inputFullName;
private TextInputLayout inputEmail;
private TextInputLayout inputPassword;
private TextInputLayout inputPasswordRepeat;
private ProgressDialog pDialog;
private SessionManager session;
private TextInputLayout inputPhone;
private TextInputLayout inputUsername;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
inputFullName = (TextInputLayout) findViewById(R.id.register_full_name);
inputEmail = (TextInputLayout) findViewById(R.id.register_email);
inputUsername = (TextInputLayout) findViewById(R.id.register_username);
inputPassword = (TextInputLayout) findViewById(R.id.register_password);
inputPasswordRepeat = (TextInputLayout) findViewById(R.id.register_pass_rep);
inputPhone = (TextInputLayout) findViewById(R.id.register_phone);
txtRegister = (Button) findViewById(R.id.register_btn);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(RegisterActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
txtRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onRegister(inputFullName, inputEmail,inputUsername,
inputPassword, inputPasswordRepeat,inputPhone);
}
});
}
public void onRegister(TextInputLayout inputFullName,
TextInputLayout inputEmail,
TextInputLayout inputUsername,
TextInputLayout inputPassword,
TextInputLayout inputPasswordRepeat,
TextInputLayout inputPhone) {
String fullName = inputFullName.getEditText().getText().toString();
if (fullName.isEmpty()) {
inputFullName.setError("User name can't be empty");
return;
}
inputFullName.setError("");
String email = inputEmail.getEditText().getText().toString();
if (email.isEmpty()) {
inputEmail.setError("User name can't be empty");
return;
}
inputEmail.setError("");
String username = inputEmail.getEditText().getText().toString();
if (username.isEmpty()) {
inputUsername.setError("User name can't be empty");
return;
}
inputUsername.setError("");
String pass = inputPassword.getEditText().getText().toString();
if (pass.isEmpty()) {
inputPassword.setError("Password can't be empty");
return;
}
inputPassword.setError("");
String passRep = inputPasswordRepeat.getEditText().getText().toString();
if (!pass.equals(passRep)) {
inputPasswordRepeat.setError("Passwords are different");
return;
}
inputPasswordRepeat.setError("");
String phone = inputPhone.getEditText().getText().toString();
if (phone.isEmpty()) {
inputPhone.setError("Password can't be empty");
return;
}
inputPhone.setError("");
UserService userService = RetrofitFishFarmApi.createService(UserService.class);
User userEntity = new User();
userEntity.setFull_name(fullName);
userEntity.setEmail(email);
userEntity.setUsername(username);
userEntity.setPassword(pass);
userEntity.setPhone(phone);
Call<User> callUserApi = userService.login(userEntity);
callUserApi.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call,
Response<User> response) {
Toast.makeText(getApplicationContext(),
"Message fro server"+response.body().getFull_name(),
Toast.LENGTH_SHORT);
}
@Override
public void onFailure(Call<User> call,
Throwable throwable) {
Toast.makeText(getApplicationContext(),
"No connection service",
Toast.LENGTH_SHORT);
}
});
//Snackbar.make(, "Register success!", Snackbar.LENGTH_LONG).show();
}
}
我的Pojo用户
package fishfarm.gebeya.fishfarm.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/ ** *由Gere于4/30/2017创建。 * /
public class User {
@SerializedName("user_id")
@Expose
private String user_id;
@SerializedName("full_name")
@Expose
private String full_name;
@SerializedName("accessToken")
@Expose
private String accessToken;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("created_at")
@Expose
private String created_at;
@SerializedName("password")
@Expose
private String password;
@SerializedName("admin")
@Expose
private boolean admin;
public String getFull_name() {
return full_name;
}
public void setFull_name(String full_name) {
this.full_name = full_name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
}
感谢您的帮助!