2017-09-29 44 views
0

我試圖使用與@FormUrlEncode改造發送POST請求,這是我的服務類改造2.3.0始終執行onFailure處

import com.dolphithronelab.siastar.response.AuthResponse; 

import java.util.List; 

import retrofit2.Call; 
import retrofit2.http.Field; 
import retrofit2.http.FormUrlEncoded; 
import retrofit2.http.GET; 
import retrofit2.http.POST; 

public interface SIAStarService { 

    @FormUrlEncoded 
    @POST("auth") 
    Call<AuthResponse>login(@Field("username") String username, @Field("password") String password); 
} 

這裏是我改造的客戶端類

import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 

public class RetrofitClient { 
    private static Retrofit retrofitClient =null; 
    public static Retrofit getClient(String apiserver){ 
     if (retrofitClient == null){ 
      retrofitClient = new Retrofit.Builder().baseUrl(apiserver).addConverterFactory(GsonConverterFactory.create()).build(); 
     } 
     return retrofitClient; 
    } 
} 

和這裏是我如何與API服務器進行通信與ApiKomunikator類

public class ApiKomunikator { 
public static final String BASE_URL = "http://10.44.7.118/siastar/public/api/"; 
public static SIAStarService getSIAStarService(){ 
    return RetrofitClient.getClient(BASE_URL).create(SIAStarService.class); 
} 
} 

,並在這裏,我如何從調用活動

siaStarService.login(mUsernameView.getText().toString(),mPasswordView.getText().toString()) 
       .enqueue(new Callback<AuthResponse>() { 
        @Override 
        public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) { 
         if(response.isSuccessful()) 
         {} 
        } 

        @Override 
        public void onFailure(Call<AuthResponse> call, Throwable t) { 
         Toast.makeText(LoginActivity.this, "Gagal koneksi ke server, periksa jaringan internet anda error: "+t.getMessage(), Toast.LENGTH_LONG).show(); 
         showProgress(false); 
        } 
       }); 

當我嘗試通過郵遞員發佈的數據,是沒有問題的,這是因爲我使用靜態IP地址或者什麼?和t.getMessage()總是空

+1

默認的超時加上't.printStackTrace();'和後堆棧跟蹤也是如此。 –

回答

0

經過長期的研究後,我知道這是一個連接超時造成的,所以我設置okhttpclient

OkHttpClient client = new OkHttpClient.Builder() 
client.setConnectTimeout(5, TimeUnit.MINUTES); 
client.setReadTimeout(5, TimeUnit.MINUTES); 
.build();