Hello I'm a newbie develop android. I am trying to learn about login with JSON. Now my server developer partner ask for me to send a JSON request like below:
{
"operation":"register",
"api_key":"123",
"user":{
"name":"Wang Jian",
"email":"wangjian0716@gmail.com",
"password":"12345678"
}
}
Now i am asked for add "api_key" to matching his server code. There are code as below Please help me to check whether it would succeed to send a JSON like above.
ServerRequest.java
public class ServerRequest {
private String operation;
private String api_key;
private User user;
public void setOperation(String operation) {
this.operation = operation;
}
public void setUser(User user) {
this.user = user;
}
public void setApi_key(String api_key) {
this.api_key = "123";
}
}
RegisterFragment.java
private void registerProcess(String name, String email,String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setName(name);
user.setEmail(email);
user.setPassword(password);
//user.setApi_key(api_key);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.REGISTER_OPERATION);
request.setApi_key(Constants.KEY);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG, "failed");
Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
Constants.java
public class Constants {
public static final String BASE_URL = "http://wangjian.site90.net/";
//public static final String BASE_URL = "http://bravonet.my/ct2/api/";
public static final String REGISTER_OPERATION = "register";
public static final String KEY = "api_key";
public static final String LOGIN_OPERATION = "login";
public static final String CHANGE_PASSWORD_OPERATION = "chgPass";
public static final String SUCCESS = "success";
public static final String FAILURE = "failure";
public static final String IS_LOGGED_IN = "isLoggedIn";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String UNIQUE_ID = "unique_id";
public static final String TAG = "Learn2Crack";
}
The code above i just modify from learn2crack original code link to Android Login Registration System with PHP and MySQL (Updated) – Server #1
I'm a really newbie at this stuff so any help will be appreciated. thanks so much!
There are my RequestInterface.java
public interface RequestInterface {
@POST("learn2crack-login-register/")
//@POST("user/")
//@POST("android/login_registration/")
Call<ServerResponse> operation(@Body ServerRequest request);
}
</div>