Je souhaite appeler un service Web Api existant dans mon application android en utilisant la bibliothèque retrofit 2 pour la connexion. Quand je débogue tout semble bien, sauf que ma ligne @POST dans l'interface de service n'est pas exécutée. J'utilise localhost et ai transmis le port, en utilisant la redirection de port en chrome, à mon téléphone.Mon instruction @POST n'est pas exécutée en utilisant Retrofit 2 dans android
Voici mon code: RestService.class
public class RestService {
public static Retrofit retrofit = null;
public static Retrofit getClient(String url) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
ApiUtils.java
public class ApiUtils {
public static final String NService = "http://localhost:5108/NSureServices.svc/";
public static Service getLoginDetails(){
return RestService.getClient(NService).create(Service.class);
}
}
Service.class
public interface Service {
@FormUrlEncoded
@POST("GetLoginDetails")
Call<User> login(@Field("USERID") String UserId, @Field("PASSWORD") String Password, @Field("apiKey") String ApiKey);
}
LoginActivity.class
public class LoginActivity extends AppCompatActivity {
EditText email, password;
Button login;
TextView register, forgotPassword;
Service service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = (EditText)findViewById(R.id.emailIdEt);
password = (EditText)findViewById(R.id.passwordEt);
register = (TextView)findViewById(R.id.signUpTv);
forgotPassword = (TextView)findViewById(R.id.forgotPasswordTv);
login = (Button)findViewById(R.id.loginBtn);
service = ApiUtils.getLoginDetails();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
User user = new User();
user.setUSERID(email.getText().toString());
user.setPASSWORD(password.getText().toString());
if(validateLogin(user.getUSERID(),user.getPASSWORD())){
doLogin(user.getUSERID(),user.getPASSWORD(),user.getApiKey());
}
}
});
}
public boolean validateLogin(String UserId, String Password){
if(UserId == null || UserId.trim().length() == 0){
Toast.makeText(this, "Username is required", Toast.LENGTH_LONG).show();
return false;
}
if(Password == null || Password.trim().length() == 0){
Toast.makeText(this, "Password is required", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
public void doLogin(String uid, String pwd, String apikey){
try {
Call<User> call =service.login(uid, pwd, apikey);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccessful()){
Toast.makeText(LoginActivity.this, "Successful", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this, "Failure", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_LONG).show();
}
});
} catch (Exception ex){
Toast.makeText(LoginActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Mes dépendances:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.android.support:support-v4:26.+'
testCompile 'junit:junit:4.12'
}
je l'ai déjà défini le apikey dans le modèle de données de l'utilisateur.
même mon @override n'est pas exécuté pour onResponse et onFailure – vinitpradhan18