我有4个类可以正常工作,但是当我尝试通过使用本示例How to get blog posts by label in blogger api更改为“通过标签获取帖子”时,出现了错误,但我的GOOGLE API KEY可以正常工作。
Webservice.java
package com.manupearls.unity.data.network;
import com.manupearls.unity.data.models.Manupost;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;
public interface Webservice {
@Headers("Cache-Control: no-cache")
@GET("posts")
Call<Manupost> getPosts(
@Query("key") String key,
@Query("pageToken") String token
);
}
MockRestAdapter.java
package com.manupearls.unity.data.network;
import com.manupearls.unity.ui.helpers.Constants;
import retrofit2.Retrofit;
import retrofit2.mock.BehaviorDelegate;
import retrofit2.mock.MockRetrofit;
import retrofit2.mock.NetworkBehavior;
public class MockRestAdapter {
public Webservice build(){
// Create a very simple Retrofit adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.build();
// Create a MockRetrofit object with a NetworkBehavior which manages the fake behavior of calls.
NetworkBehavior behavior = NetworkBehavior.create();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
.networkBehavior(behavior)
.build();
BehaviorDelegate<Webservice> delegate = mockRetrofit.create(Webservice.class);
return new MockWebservice(delegate);
}
}
Constants.Java
package com.manupearls.unity.ui.helpers;
public class Constants {
public static String BLOGGER_KEY = "";
public static String BLOGGER_ID = "2331406457493300588";
public static String LABELS = "firmware";
public static String BASE_URL = "https://www.googleapis.com/blogger/v3/blogs/"+BLOGGER_ID+"/";
}
RestAdapter.Java
package com.manupearls.unity.data.network;
import android.util.Log;
import com.manupearls.unity.data.models.Manupost;
import com.manupearls.unity.ui.helpers.Constants;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Url;
public class RestAdapter {
static Retrofit retrofit;
static OkHttpClient okHttpClient;
public static Webservice createAPI() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(5, TimeUnit.SECONDS);
builder.writeTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(30, TimeUnit.SECONDS);
builder.cache(null);
okHttpClient = builder.build();
retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(Webservice.class);
}
public static void cancel(){
okHttpClient.dispatcher().cancelAll();
}
public static Retrofit getRetrofit(){
return retrofit;
}
}
当我将Webservice.java代码@GET(“ posts”)更改为@GET(“ https://www.googleapis.com/blogger/v3/blogs/2331406457493300588/posts?labels=firmware&key=“)工作正常,但不能随标签一起返回