如何在Android中通过改造进行单次呼叫

时间:2019-04-02 16:52:46

标签: java android retrofit

如何进行翻新的单次呼叫,我的意思是我不希望回叫请求,我需要发出一个请求和另一个请求,直到用户滑动刷新(我已经使它滑动刷新)为止。但是此代码使回调超出了我的Google API。仅通过测试应用程序

我尝试查看一些以前的帖子和代码,但不能解决我的问题

PostRepositoryfirmware.Java


import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.util.Log;import com.manupearls.unity.data.models.OcoPostsfirmware;
import com.manupearls.unity.data.network.Webservicefirmware;
import com.manupearls.unity.ui.helpers.Constantsfirmware;
import java.io.IOException;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class PostRepositoryfirmware {
private final Webservicefirmware webservicefirmware;
private OkHttpClient okHttpClient;
this.webservicefirmware = webservicefirmware;
}
public LiveData<OcoPostsfirmware> getPosts(String token) {
final MutableLiveData<OcoPostsfirmware> data = new MutableLiveData<>();
webservicefirmware.getPosts( Constantsfirmware.BLOGGER_KEY,Constantsfirmware.FIRMWARE,token).enqueue( new Callback<OcoPostsfirmware>() {
@Override
public void onResponse(Call<OcoPostsfirmware> call,Response<OcoPostsfirmware> response) {
if (response.isSuccessful()){
data.setValue(response.body());
}else{
try {
Log.d("error networking", response.errorBody().string());
} catch (IOException e) {
 e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<OcoPostsfirmware> call,Throwable t) {
Log.d("errorss networking", t.getMessage());
}
});
return data;
}
}

我还有另一个班级

RestAdapterfirmware.Java


import com.manupearls.unity.ui.helpers.Constantsfirmware;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RestAdapterfirmware {
static Retrofit retrofit;
static OkHttpClient okHttpClient;
public static Webservicefirmware createAPI() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(60, TimeUnit.SECONDS);
builder.writeTimeout(30, TimeUnit.SECONDS);
builder.readTimeout(60, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(false)
.cache(null);
okHttpClient = builder.build();
retrofit = new Retrofit.Builder()
.baseUrl( Constantsfirmware.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create( Webservicefirmware.class);  
}
public static void cancel(){
okHttpClient.dispatcher().cancelAll();
}
public static Retrofit getRetrofit(){
return retrofit;
}
}

这是Webservicefirmware.java

import com.manupearls.unity.data.models.OcoPostsfirmware;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;


public interface Webservicefirmware {
    @Headers("Cache-Control: no-cache")
    @GET("posts")
    Call<OcoPostsfirmware> getPosts(
            @Query("key") String key,
            @Query("labels") String labels,
            @Query("pageToken") String token
    );
}

0 个答案:

没有答案