无法通过改型接收数据

时间:2018-09-28 06:33:01

标签: android retrofit2

我正在使用改造来获取Json数据,但是出了点问题。没有数据被接收。

以下是我的模型课

public class DataList {
@SerializedName("pariyojanaId")
    @Expose
    private Float pariyojanaId;
    @SerializedName("projectStatus")
    @Expose
    private String projectStatus;
    @SerializedName("heading")
    @Expose
    private String heading;
    @SerializedName("projectNumber")
    @Expose
    private Float projectNumber;
    @SerializedName("fieldTitle")
    @Expose
    private String fieldTitle;
    @SerializedName("targetedGroup")
    @Expose
    private String targetedGroup;

//Getter Setter
}

我的界面

public interface PariyojanaInterface {
@GET("projectBudget/pariyojanaListForMobile")
Call<List<DataList>> getData(
        @Query("memberId") int id);
}

我有一个正在工作的API接口。 下面是我的改造功能。

 apiInterface = ApiClient.getApiClient().create(PariyojanaInterface.class);
    Call<List<DataList>> call = apiInterface.getData(id);
    Log.e("url", call.request().toString());
    call.enqueue(new Callback<List<DataList>>() {
        @Override
        public void onResponse(Call<List<DataList>> call, Response<List<DataList>> response) {
            Log.e("url", "hello");
            posts =  response.body();
            adapter = new PariyojanaAdapter(posts,getApplicationContext());
            recyclerView.setAdapter(adapter);

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<List<DataList>> call, Throwable t) {
            Log.e("error",t.getMessage());
        }
    });

当我运行该程序时,一直在线

Log.e("url", call.request().toString());

我得到了回应,但随后得到了回应

call.enqueue(new Callback<List<DataList>>() {}

我认为有一些问题。我无法运行此程序。有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

尝试此流程

               try {
                    JSONObject jsonObject = new JSONObject(resultJSONResponse);

                    JSONArray jsonArray = jsonObject.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++)
                    {
                        JSONObject o = jsonArray.getJSONObject(i);

                                strid = o.getString("pariyojanaId");
                                strstatus = o.getString("projectStatus");

                                showMethod();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

答案 1 :(得分:1)

您不能不通过外部对象就直接访问数组,您将需要一个类似于以下的pojo类:

DataResponse.java

    public class DataResponse {

@SerializedName("data")
@Expose
private List<DataList> data = null;

public List<DataList> getData() {
return data;
}

public void setData(List<DataList> data) {
this.data = data;
}

}

将所有List<DataList>更改为DataResponse,即像这样转换您的网络通话:

apiInterface = ApiClient.getApiClient().create(PariyojanaInterface.class);
Call<DataResponse> call = apiInterface.getData(id);
Log.e("url", call.request().toString());
call.enqueue(new Callback<DataResponse>() {
    @Override
    public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
        Log.e("url", "hello");
        ArrayList<DataList> post_list =  new ArrayList<>(response.body().getData());
        adapter = new PariyojanaAdapter(post_list,getApplicationContext());
        recyclerView.setAdapter(adapter);

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<DataResponse> call, Throwable t) {
        Log.e("error",t.getMessage());
    }
});

并更改这样的界面

    public interface PariyojanaInterface {
@GET("projectBudget/pariyojanaListForMobile")
Call<DataResponse> getData(
        @Query("memberId") int id);
}

正如我所提到的,您的端点返回一个对象,而不是数组。因此,请指定一个对象作为网络回调的返回参数