JonathanNet JonathanNet - 13 days ago 9
reST (reStructuredText) Question

Retrofit2 login

I am trying to convert my login to retrofit2 my old LoginActivity here:

public class LoginActivity extends Activity {
private static final String TAG = RegisterActivity.class.getSimpleName();

// GPSTracker class
GPSTracker gps;

private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);

// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);

// SQLite database handler
db = new SQLiteHandler(getApplicationContext());

// 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(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();

// Check for empty data in the form
if (!email.isEmpty() && !password.isEmpty()) {
// login user
checkLogin(email, password);
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
R.string.login_pleaseentercredentials, Toast.LENGTH_LONG)
.show();
}
}

});

// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});

}

/**
* function to verify login details in mysql db
* */
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";

pDialog.setMessage(getResources().getString(R.string.login_txt_loggingin));
showDialog();

StringRequest strReq = new StringRequest(Method.POST,
AppConfig.API_URL + "/login", new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();

try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");

// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session

// Now store the user in SQLite
String uid = jObj.getString("uid");

String name = jObj.getString("name");
String email = jObj.getString("email");
String created_at = jObj.getString("created_at");
String apiKey = jObj.getString("apiKey");
String status = jObj.getString("status");

if(status.equals("1")) {
// Launch main activity
Intent intent = new Intent(LoginActivity.this, UserBlockedActivity.class);
startActivity(intent);
finish();
} else {
session.setLogin(true);

// Inserting row in users table
db.addUser(name, email, uid, created_at, apiKey);

// Launch main activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("message");

if (errorMsg.equals("login_incorrectcredentials")) {
Toast.makeText(getApplicationContext(), R.string.login_incorrectcredentials, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);

return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}

private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}


My new LoginActivity

public class LoginActivity extends AppCompatActivity {

private static final String TAG = LoginActivity.class.getSimpleName();

private EditText inputEmail;
private EditText inputPassword;

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

inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
}

// When Button Login clicked
public void Signin(View v) {

String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();

//Calling method of field validation
if (CheckFieldValidation()) {

ApiInterface apiService =
RestClient.getClient().create(ApiInterface.class);

HashMap<String, String> parameters = new HashMap<>();
parameters.put("email", email);
parameters.put("password", password);

//Calling method to get check login

Call<LoginModal> call = apiService.Login(parameters);
call.enqueue(new Callback<LoginModal>() {
@Override
public void onResponse(Call<LoginModal> call, Response<LoginModal> response) {
if (response.body().getStatus().equals("1")) {
Toast.makeText(LoginActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Invalid UserName/Pass ", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<LoginModal> call, Throwable t) {
// something went completely south (like no internet connection)
Toast.makeText(LoginActivity.this, "An error occurred. Please try again", Toast.LENGTH_SHORT).show();
Log.d("Error", t.getMessage());
}
});

}
}

//checking field are empty
private boolean CheckFieldValidation() {

boolean valid = true;
if (inputEmail.getText().toString().equals("")) {
inputEmail.setError("Can't be Empty");
valid = false;
} else if (inputPassword.getText().toString().equals("")) {
inputPassword.setError("Can't be Empty");
valid = false;
}

return valid;

}
}


And my RestClient looks like this:

public class RestClient {

public static final String BASE_URL = "http://api.fitnessm8.dk/v2/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {
if (retrofit==null) {

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
//.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addNetworkInterceptor(new Interceptor() {

@Override

public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
// .addHeader(Constant.Header, authToken)
.build();
return chain.proceed(request);
}
}).build();

retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
}


ApiInterface.java

public interface ApiInterface {
@FormUrlEncoded
@POST("login")
Call<LoginModal> Login(@Body HashMap<String, String> parameters);
}


LoginModal.java

public class LoginModal {

@SerializedName("id")
@Expose
private String id;
@SerializedName("fullname")
@Expose
private String fullname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("api_key")
@Expose
private String apiKey;
@SerializedName("status")
@Expose
private String status;
@SerializedName("created_at")
@Expose
private String createdAt;

/**
*
* @return
* The id
*/
public String getId() {
return id;
}

/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

/**
*
* @return
* The fullname
*/
public String getFullname() {
return fullname;
}

/**
*
* @param fullname
* The fullname
*/
public void setFullname(String fullname) {
this.fullname = fullname;
}

/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
*
* @return
* The apiKey
*/
public String getApiKey() {
return apiKey;
}

/**
*
* @param apiKey
* The api_key
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

/**
*
* @return
* The status
*/
public String getStatus() {
return status;
}

/**
*
* @param status
* The status
*/
public void setStatus(String status) {
this.status = status;
}

/**
*
* @return
* The createdAt
*/
public String getCreatedAt() {
return createdAt;
}

/**
*
* @param createdAt
* The created_at
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
}


Then I try to login on the app I get a error:

Caused by: java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #1)

I am new into android development so what I thing is wrong is in the ApiInterface with the Body, but how to I get it to work? :)

I am using the Rest Api from androidhive, this code but I modified it, to fit my app:

http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/

Answer

When you add @FormUrlEncoded, the data is sent in the form param1=value1&param2=value2 ie form encoding. When you use @Body, the parameter is converted to JSON and sent. Depending on your server side code, you will want to choose one, not both.

Comments