Я пытаюсь получить данные из API, где я должен использовать метод post как новичок. У меня нет опыта работы с методом post дооснащения, но из других вопросов StackOverflow я могу получить код, но когда я ставлюимя пользователя и пароль Я получаю сообщение об ошибке 400, поскольку я не могу передать данные на сервер. Вот код
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;
}
Я получаю такую ошибку
`Response {protocol = http / 1.1, code = 400, message = Bad Request, url = http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php}'
Любое направление, что я делаю неправильно, будет полезно {"username": "test", "password": "123456"} пример вывода, который я должен получить
{"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011\/04\/25\",\"$320,800\"]...
Метод действия:
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:
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();
}
}
данные: