2017-09-15 45 views
0

中使用Retrofit 2執行我想在我的android應用程序中使用retrofit 2庫登錄時調用現有的Web Api服務。當我調試一切似乎很好,除了我的@POST行服務接口不執行。我正在使用本地主機,並使用chrome中的端口轉發將端口轉發到我的手機。我的@POST語句沒有在android

這裏是我的代碼: 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

即使我的@override沒有被執行onResponse和onFailure – vinitpradhan18

回答

0

您在一開始錯過斜線你網址

你的URL應該是這樣的:

@POST("/GetLoginDetails/") 

而且在你的依賴,你已經使用支持-V4和V7這是沒有必要的。

support-v7包括v4支持庫所以compile 'com.android.support:support-v4:26.+'不需要在您的依賴項中。

不要在您的依賴版本中使用+運算符。用戶當前版本的支持庫。

+0

我做到了。它沒有工作 – vinitpradhan18

+0

你可以嘗試添加一個斜槓末尾 –

+0

已經做到了。它不起作用 – vinitpradhan18