android graphQL阿波罗
我正在使用apollo GraphQL进行API响应。我已经在拦截器类中设置了令牌。在主要活动中,我使用apolloClient,在其中我传递查询以获取数据 我正在从拦截器类获取API响应,但没有获取查询响应android graphQL阿波罗,android,interceptor,graphql,apollo,Android,Interceptor,Graphql,Apollo,我正在使用apollo GraphQL进行API响应。我已经在拦截器类中设置了令牌。在主要活动中,我使用apolloClient,在其中我传递查询以获取数据 我正在从拦截器类获取API响应,但没有获取查询响应 public class HttpInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException {
public class HttpInterceptor implements Interceptor {
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("okhttp");
String BASE_URL = "url_example.json";
Response response = null;
Request request = chain.request();
request =chain.request();
request = request.newBuilder()
.url(BASE_URL)
.header("token_key", "token_value")
.build();
long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
response = chain.proceed(request);
long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
申请类别代码:
private static final String BASE_URL = "url_example.json";
private ApolloClient apolloClient;
@Override public void onCreate() {
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpInterceptor())
.build();
apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
}
public ApolloClient apolloClient() {
return apolloClient;
}
代码或主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
application = (SampleApplication) getApplication();
content = (ViewGroup) findViewById(R.id.rl_content_holder);
progressBar = (ProgressBar) findViewById(R.id.loading_bar);
RecyclerView collectionRecyclerView = (RecyclerView) findViewById(R.id.rv_collection_list);
mCollectionAdapter = new CollectionAdapter(this);
collectionRecyclerView.setAdapter(mCollectionAdapter);
collectionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
fetchCollection();
}
ApolloCall.Callback<ShopifyCollections.Data> mDataCallback =
new ApolloCall.Callback<ShopifyCollections.Data>() {
@Override
public void onResponse(@Nonnull com.apollographql.apollo.api.Response<ShopifyCollections.Data> response) {
Log.i("Shopify CallBack",response.toString());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Log.e(TAG, e.getMessage(), e);
}
};
public void fetchCollection(){
mHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpInterceptor())
.build();
ShopifyCollections collectionQuery = ShopifyCollections.builder()
.limit(250)
.build();
collectionCall = application.apolloClient().newCall(collectionQuery);
collectionCall.enqueue(mDataCallback);
}
我看到您正在尝试使用Shopify店面GraphQL模式编写示例?如果是,您可以在以下网址找到示例: 它还没有完成,但可以给你一些概述 如果问题与如何将身份验证令牌附加到标头有关:
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder().method(original.method(), original.body());
builder.header("User-Agent", "Android Apollo Client");
builder.header("X-Shopify-Storefront-Access-Token", shopifyApiKey);
return chain.proceed(builder.build());
})
.build();
apolloClient = ApolloClient.builder()
.okHttpClient(httpClient)
.serverUrl(HttpUrl.parse("https://" + shopUrl + "/api/graphql"))
.withCustomTypeAdapter(CustomType.MONEY, new CustomTypeAdapter<BigDecimal>() {
@Override public BigDecimal decode(final String value) {
return new BigDecimal(value);
}
@Override public String encode(final BigDecimal value) {
return value.toString();
}
})
.build();
OkHttpClient-httpClient=new-OkHttpClient.Builder()
.addInterceptor(链->{
Request original=chain.Request();
Request.Builder=original.newBuilder().method(original.method(),original.body());
标题(“用户代理”、“安卓阿波罗客户端”);
builder.header(“X-Shopify-Storefront-Access-Token”,shopifyApiKey);
返回链。继续(builder.build());
})
.build();
apolloClient=apolloClient.builder()
.okHttpClient(httpClient)
.serverUrl(HttpUrl.parse(“https://“+shopUrl+”/api/graphql”))
.withCustomTypeAdapter(CustomType.MONEY,新的CustomTypeAdapter(){
@重写公共BigDecimal解码(最终字符串值){
返回新的BigDecimal(值);
}
@重写公共字符串编码(最终的BigDecimal值){
返回值.toString();
}
})
.build();
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder().method(original.method(), original.body());
builder.header("User-Agent", "Android Apollo Client");
builder.header("X-Shopify-Storefront-Access-Token", shopifyApiKey);
return chain.proceed(builder.build());
})
.build();
apolloClient = ApolloClient.builder()
.okHttpClient(httpClient)
.serverUrl(HttpUrl.parse("https://" + shopUrl + "/api/graphql"))
.withCustomTypeAdapter(CustomType.MONEY, new CustomTypeAdapter<BigDecimal>() {
@Override public BigDecimal decode(final String value) {
return new BigDecimal(value);
}
@Override public String encode(final BigDecimal value) {
return value.toString();
}
})
.build();