首页 文章

改造2.3.0总是执行onFailure

提问于
浏览
0

我正在尝试使用@FormUrlEncode改造发送帖子请求,这是我的服务类

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;
    }
}

以下是我使用ApiKomunikator类与api服务器通信的方式

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()始终为null

1 回答

  • 0

    经过长时间的研究,我知道这是由超时连接引起的,所以我在okhttpclient上设置了默认超时

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

相关问题