Reputation: 1251
I am trying to get data from an API where I have to use post method as a beginner I have no experience with the post method of retrofit but from other other StackOverflow questions I am able to get the code done but when I put the username and password I am getting an error 400 as I am not able to pass data to the server here is the code
APIInterface:
public interface APIInterface {
@POST("http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@Body LoginResponse login);
}
APIClient:
class APIClient {
public static final String BASE_URL = "http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php/";
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;
}
}
getter-setter:
public class LoginResponse {
@SerializedName("username")
public String UserId;
@SerializedName("FirstName")
public String FirstName;
@SerializedName("LastName")
public String LastName;
@SerializedName("ProfilePicture")
public String ProfilePicture;
@SerializedName("Password")
public String Password;
public LoginResponse(String UserId, String Password) {
this.UserId = UserId;
this.Password = Password;
}
I am getting an error like this
`Response{protocol=http/1.1, code=400, message=Bad Request, url=http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php}'
Any direction what i am doing wrong will be helpful { "username":"test", "password":"123456" } sample output that i must get
{"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011\/04\/25\",\"$320,800\"]...
Activity method:
private void loginRetrofit2Api(String userId, String password) {
final LoginResponse login = new LoginResponse(userId, password);
Call<LoginResponse> call1 = apiInterface.createUser(login);
call1.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e("Login", response.toString());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
public boolean checkValidation() {
userId = et_Email.getText().toString();
password = et_Pass.getText().toString();
if (et_Email.getText().toString().trim().equals("")) {
CommonMethod.showAlert("UserId Cannot be left blank", MainActivity.this);
return false;
} else if (et_Pass.getText().toString().trim().equals("")) {
CommonMethod.showAlert("password Cannot be left blank", MainActivity.this);
return false;
}
return true;
}
CommonMethod class:
public static boolean isNetworkAvailable(Context ctx) {
ConnectivityManager connectivityManager
= (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public static void showAlert(String message, Activity context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
try {
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Views: 756
Reputation: 8853
You are doing few things wrong here.
First you need to set your base url properly.
public static final String BASE_URL = "http://tvsfit.mytvs.in/"
Now change path in method call accordingly. You need to set path that will be added after Base URL.
public interface APIInterface {
@POST("reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@Body LoginResponse login);
}
Serialized names are case sensitive and you need to match them exactly with your parameter names.
@SerializedName("password") //in sample request you are using password and not Password
public String Password;
Upvotes: 1
Reputation: 1
have you test this api at Postman whether it is running well or not when i test this api at postman . it shows error 400 and shows error message {"ErrorDescription":"Mandatory parameters missing","Error":400} .it' you are not passing all the parameters for that api call. see this screenshot . api error screenshot
Upvotes: 0
Reputation: 150
What you should be doing is something as:
public interface APIInterface {
@POST("http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@String userId, @String password);
}
The LoginResponse is your class to handle the response. Therefore, it doesn't make much sense passing as the argument when trying to make login. Your would look like:
Call<LoginResponse> call1 = apiInterface.createUser(userId, password);
I might be wrong of course, so verify with the documentation in order to know exactly all the arguments.
Upvotes: 0