【问题标题】:How to parse parameters using get method with Retrofit?如何使用带有 Retrofit 的 get 方法解析参数?
【发布时间】:2018-01-02 12:45:33
【问题描述】:

我想传递 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 工作截图

【问题讨论】:

  • API 是否在 postman 中工作?
  • @R2R 不,它不起作用。
  • 那是API的问题。请检查
  • 你能更新工作屏幕截图吗
  • @R2R 请检查我已添加屏幕截图

标签: android get retrofit2


【解决方案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); 

 }

【讨论】:

  • 获取此登录失败:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $
  • notificationJSON() 这是我的解析方法,请告诉我有什么问题
  • 当我调试.. request.getNotification("1","10");它在 call.enqueue() 中转到 onFailure()
  • 知道了...应该是 BEGIN_ARRAY 但在第 1 行第 2 列路径 $
【解决方案2】:

这对我有用:

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

【讨论】:

  • 得到这种类型的日志 .. onFailure: 预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $
猜你喜欢
  • 2016-01-30
  • 2018-10-27
  • 2021-07-10
  • 1970-01-01
  • 2018-05-24
  • 2020-07-02
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多