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

问题描述 投票:0回答:2

我想传递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

android get retrofit2
2个回答
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); 

 }

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); 
}

get notification.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;
  }
} 
© www.soinside.com 2019 - 2024. All rights reserved.