【发布时间】:2020-10-30 23:09:57
【问题描述】:
我有一个没有任何对象(键)的 JSON 数组,其中有这样的 JSON 对象:
[ 137, 80, 78, 71, 13, 10, 26, 10 ]
我尝试解析它,但找不到成功,谁能建议我如何使用 Retrofit 解析这种类型的响应?
到目前为止,我所做的是在我做过的活动中:-
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("userName", "none\\\\Android");
Call call = api.getUserIcon(jsonObject);
// Displaying the user a loader with the specific message
dialog.setMessage("Loading... Please wait...");
dialog.show();
call.enqueue(new Callback<Integer>() {
@Override
public void onResponse(Call<Integer> call, Response<Integer> response) {
if (response.isSuccessful()) {
if (dialog.isShowing())
dialog.dismiss();
} else {
if (dialog.isShowing())
dialog.dismiss();
// if successfully not added
Toast.makeText(getActivity(), "Failure in Success Response", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Integer> call, Throwable t) {
if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(getActivity(), "Failure in Parsing", Toast.LENGTH_SHORT).show();
}
});
在我的界面中:-
@Headers({
"Content-Type:application/json; charset=utf-8",
"Content-Encoding:UTF-8",
"Authorization:Basic bnhvbmVcS2Fua2FTZW46NllrKkNpezc=",
"appID:Sample Android App",
"locale:en-US"
})
@POST("Admin/GetRegisteredUserIcon")
Call<List<Integer>> getUserIcon(
@Body JsonObject body);
【问题讨论】:
-
在改造界面中向我们展示您尝试了什么
-
@ErwinKurniawanA:我已经编辑了
-
您是否尝试过使用 List
,因为响应都是数字? -
@ErwinKurniawanA:更改后,我仍然在失败中的第 1 行第 1 列路径 $ 处输入 End of
标签: android json retrofit okhttp