使用retofit和Moshi不能完全解析api响应“ json”

时间:2018-12-03 19:01:25

标签: android json api retrofit2 moshi

我找到了api,我想尝试一下,但是响应不是“ full json”,所以当我得到响应时,我得到了解析器错误。

像往常一样,我正在使用翻新,所以重要的部分或多或少是这样的:

val retrofit = Retrofit.Builder()
            .baseUrl(AwsomeAPIConstants.HOST)
            .addConverterFactory(MoshiConverterFactory.create())
            .build()

        awsomeApi = retrofit.create(AwsomeaApiService::class.java)

正如我所说,API的所有响应都像:<sentence> = {<json>},显然MoshiConverter无法解析它。

一些回应示例:

info={'timestamp':1292608331,'error':0}

info={'status':1,'error':0}

search={'error':1}

有什么办法可以解析它?

1 个答案:

答案 0 :(得分:0)

创建一个响应主体转换器,该转换器获取响应主体的源,跳过前导字节并进行委托。 这是一个示例,带有添加的注释以标记在响应主体中具有此前缀的服务方法。

final class Foo {
  long timestamp;
  int error;
}

interface FooService {
  @EqualsJson @GET("/") Call<Foo> foo();
}

@Test public void foo() throws IOException {
  MockWebServer server = new MockWebServer();
  server.enqueue(new MockResponse().setBody("info={'timestamp':1292608331,'error':0}"));
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(server.url("/"))
      .addConverterFactory(new EqualsJson.ResponseBodyConverterFactory())
      .addConverterFactory(MoshiConverterFactory.create().asLenient())
      .build();
  FooService fooService = retrofit.create(FooService.class);

  Call<Foo> call = fooService.foo();
  Response<Foo> response = call.execute();
  Foo body = response.body();
  assertThat(body.timestamp).isEqualTo(1292608331);
  assertThat(body.error).isEqualTo(0);
}

@Retention(RUNTIME)
public @interface EqualsJson {
  final class ResponseBodyConverterFactory extends Converter.Factory {
    @Nullable
    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(
        Type type, Annotation[] annotations, Retrofit retrofit) {
      for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        if (annotation instanceof EqualsJson) {
          Annotation[] nextAnnotations = new Annotation[annotations.length - 1];
          System.arraycopy(
              annotations, 0, nextAnnotations, 0, i);
          System.arraycopy(
              annotations, i + 1, nextAnnotations, i, annotations.length - i - 1);
          return new ResponseBodyConverter(
              retrofit.nextResponseBodyConverter(this, type, nextAnnotations));
        }
      }
      return null;
    }

    static final class ResponseBodyConverter implements Converter<ResponseBody, Object> {
      static final ByteString JSON_PREFIX = ByteString.encodeUtf8("=");

      final Converter<ResponseBody, ?> delegate;

      ResponseBodyConverter(Converter<ResponseBody, Object> delegate) {
        this.delegate = delegate;
      }

      @Override
      public Object convert(ResponseBody value) throws IOException {
        BufferedSource source = value.source();
        source.skip(source.indexOf(JSON_PREFIX) + 1);
        return delegate.convert(
            ResponseBody.create(value.contentType(), value.contentLength(), source));
      }
    }
  }
}