In the following code I am trying to get 3 strings(username, password and email) from the user inside RegisterFragment
and pass it to the server via Retrofit
library inside android studio. But I get Error 400
. I know it should work as it is one of our homework and the other guys did it successfully, but I don't know which part of my code is wrong!
I tried to debug the program and inside RegisterController.java
within onFailure
function it says:
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();
}
}
ChatRoomApi :
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);
}
}
RegisterController:
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());
}
});
}
}
RegisterFragment:
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() {
}
}
Edit : Inside the postman software, I can send this query:
{
"username":"xyzxyzxyz",
"password":"xyzxyzxyz",
"email":"xyzxyzxyz@gmail.com"
}
And get this result(code 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"
}
Maybe it's stupid, but I changed this line of code private String userName;
to private String username;
inside the User.java
and it worked!
As you can see here
{
"username":"xyzxyzxyz",
"password":"xyzxyzxyz",
"email":"xyzxyzxyz@gmail.com"
}
The API gets username
not userName
!