如何使用带有Retrofit的get方法解析参数?

时间:2018-01-02 12:45:34

标签: android get retrofit2

我想传递2个参数并通过Retrofit获取数据但是我在日志中关注Response

code...404 message...Not Found body...null

下面是我正在尝试的代码。

这是我的界面

public interface RequestInterface { 
    String NOTIFICATION_URL = "http://xxxxx/api/";  
    @GET("Image/GetNotificationList/{PageNumber}/{PageSize}")
    Call<List<GetNotification>> getNotification(@Path("PageNumber")  String PageNumber, @Path("PageSize") String PageSize); 
}

当我拨打RequestInterface

private void notificationJSON() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.NOTIFICATION_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<List<GetNotification>> call = request.getNotification("1","10");

        call.enqueue(new Callback<List<GetNotification>>() {
            @Override
            public void onResponse(Call<List<GetNotification>> call, Response<List<GetNotification>> response) {
                List<GetNotification> notification_pojo = response.body();
                Log.d("Message", "code..."+response.code() + " message..." + response.message()+" body..."+response.body());
                Log.d("Message pojo", "notification_pojo = " +notification_pojo);
            }

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

这是我的Api Response

[  
   {  
      "PageUrl":"ureeee",
      "CircularDetailId":1,
      "Subject":"suBJECT",
      "CircularPath":"/UploadDocument/3phasehindi.pdf",
      "Description":"Description"
   },
   ......
]

Api工作截图

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用query params更改而不是Path

 public interface RequestInterface { 

 String NOTIFICATION_URL = "http://xxxxx/api/";  

 @GET("Image/GetNotificationList")
 Call<List<GetNotification>> getNotification(@Query("PageNumber")  String 
     PageNumber, @Query("PageSize") String PageSize); 

 }

答案 1 :(得分:0)

这对我有用:

public interface RequestInterface { 
String NOTIFICATION_URL = "http://xxxxx/api/";  
@GET("Image/GetNotificationList")
Call<List<GetNotification>> getNotification(@Query("PageNumber")  String PageNumber, @Query("PageSize") String PageSize); 
}

<强> GetNotification.java

public class GetNotification{

  @SerializedName("PageUrl")
  @Expose
  private String pageUrl;
  @SerializedName("CircularDetailId")
  @Expose
  private Integer circularDetailId;
  @SerializedName("Subject")
  @Expose
  private String subject;
  @SerializedName("CircularPath")
  @Expose
  private String circularPath;
  @SerializedName("Description")
  @Expose
  private String description;

  public String getPageUrl() {
     return pageUrl;
  }

  public void setPageUrl(String pageUrl) {
      this.pageUrl = pageUrl;
  }

  public Integer getCircularDetailId() {
      return circularDetailId;
  }

  public void setCircularDetailId(Integer circularDetailId) {
    this.circularDetailId = circularDetailId;
  }

  public String getSubject() {
    return subject;
  }

  public void setSubject(String subject) {
     this.subject = subject;
  }

  public String getCircularPath() {
     return circularPath;
  }

  public void setCircularPath(String circularPath) {
       this.circularPath = circularPath;
  }

  public String getDescription() {
       return description;
  }

  public void setDescription(String description) {
      this.description = description;
  }
}