2017-09-29 43 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();