Android解析在改造中使用相同的变量对象名称

时间:2017-03-08 07:10:17

标签: android json retrofit retrofit2 gson

我见过很多关于改造的例子。而且我已经完成了大部分像listview这样的事情,但这是我不容易找到的新事物。我正在使用retrofit2.Here它有相同的名称详细信息首先是简单的对象,其他有嵌套对象,我有点混淆如何解析它。它是alogin形式首先它检查细节是否是一个然后它进一步。

[
 {
"title": "success",
"details": "1"
},
{
"title": "data",
"details": {
  "EMPLOYEE_ID": 5,
  "FIRST_NAME": "ABHISHEK",
  "LAST_NAME": "BARDOLIA"

  }
 }
]

以下是用于登录目的的界面。

  public interface LoginInterface {
  @GET("Login")
  Call<List<ClsLogin>> Login(@Query("username") String username
        , @Query("password") String password
        , @Query("imei") String imei);
}

模特类:      公共类ClsLogin {

@SerializedName("title")
String title;

@SerializedName("details")
String details;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
 }
}

回应代码:

public static void login(final LoginCallback logincallback, ClsLogin ObjLogin) {
    LoginInterface apiLoginService = ApiClient.getClient().create(LoginInterface.class);

    Call<List<ClsLogin>> call = apiLoginService.Login(
            ObjLogin.getUsername(),
            ObjLogin.getPassword(),
            ObjLogin.getImei());
    Log.e("response", String.valueOf(call.request().url()));


call.enqueue(new Callback<List<ClsLogin>>() {
    @Override
    public void onResponse(Call<List<ClsLogin>> call, Response<List<ClsLogin>> response) {
        Log.e("response", "onResponse ");
    }

    @Override
    public void onFailure(Call<List<ClsLogin>> call, Throwable t) {
        Log.e("response", t.getLocalizedMessage());
    }
});


}

1 个答案:

答案 0 :(得分:0)

您可以看到GsonConverterFactory解析数据的方式:

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
  Retrofit retrofit) {
     TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
     return new GsonResponseBodyConverter<>(adapter);
}

GsonResponseBodyConverter如何运作:

    @Override public T convert(ResponseBody value) throws IOException {
    try {
      return adapter.fromJson(value.charStream());
    } finally {
      value.close();
    }
  }

因此您可以编写自己的ResponseBodyConverter来区分数据和状态代码,您只需要覆盖convert方法。 因为,GsonConverterFactory和GsonRequestBodyConverter是finnal类,你应该在你的项目中复制它们。你应该编写你的Custom GsonResponseBodyConverter:

 final class CusGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final TypeAdapter<T> adapter;
    private final Gson gson;

    CusGsonResponseBodyConverter(TypeAdapter<T> adapter) {
        this.adapter = adapter;
        this.gson = new Gson();
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        //get the response string data
        String response = value.string();

        //in there,you can check your data
        // for exp,you can catch the exception and return the entity with error flag
        //or just return the gson entity,like this:
        MediaType contentType = value.contentType();
        Charset charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
        InputStream inputStream = new ByteArrayInputStream(response.getBytes());
        Reader reader = new InputStreamReader(inputStream, charset);
        JsonReader jsonReader = gson.newJsonReader(reader);

        try {
            return adapter.read(jsonReader);
        } finally {
            value.close();
        }
    }
}