我想在我的Android应用程序中调用现有的Web Api服务,使用retrofit 2库进行登录。当我调试一切似乎没有问题,只是我的@POST行没有执行服务接口。我正在使用localhost,并使用chrome中的端口转发将端口转发到我的手机。
这是我的代码: 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();
}
}
}
我的依赖项:
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'
}
我已经在用户数据模型中定义了apikey。
答案 0 :(得分:0)
你在网址的开头错过了斜线
您的网址应如下所示:
@POST("/GetLoginDetails/")
同样在您的依赖项中,您使用了support-v4和v7,这是不必要的。
support-v7包含v4支持库,因此您的依赖项中不需要compile 'com.android.support:support-v4:26.+'
。
不要在依赖版本中使用+运算符。用户当前版本的支持库。