【问题标题】:How to send post data json to api using retrofit getting response as object如何使用改造获取响应作为对象将发布数据 json 发送到 api
【发布时间】:2018-07-26 10:46:19
【问题描述】:

邮递员描述:

界面:

public interface PostService {
    @Headers("Content-Type: application/json")
    @FormUrlEncoded
    @POST("/user/login")
    Call<User> login(@Body User user);
}

调用函数:

User user = new User("student", "student@gmail.com");
Call<User> noticeList = RetrofitAPI.getService().login(user);

noticeList.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        Log.i("postdata", "v =" + response.body().getId());
    }

    @Override
    public void onFailure(Call<User> call, Throwable throwable) {
        Toast.makeText(ActivityLogin.this, "Server taking time try refreshing", Toast.LENGTH_SHORT).show();
    }
});

我们如何将json post数据发送到服务器?

【问题讨论】:

    标签: android retrofit2 gson


    【解决方案1】:

    您需要将Call&lt;User&gt; login(@Body User user); 替换为Call&lt;YOURRESPONSEMODEL&gt; login(@Body User user); 之类的响应模型类,并相应地更改您的call function。要创建响应模型,请使用邮递员的回复并创建模型here

    【讨论】:

      【解决方案2】:

      首先,创建 Pojo 类:

      public class SubscriptionResponseModel {
      
          @SerializedName("mNo")
          @Expose
          private String mNo;
      
          @SerializedName("pId")
          @Expose
          private String pId;
          @SerializedName("status")
          @Expose
          private String status;
      
          public String getPId() {
              return pId;
          }
      
          public void setPId(String pId) {
              this.pId = pId;
          }
      
          public String getStatus() {
              return status;
          }
      
          public void setStatus(String status) {
              this.status = status;
          }
      
      }
      

      然后,创建接口:

      @POST("subs?")
      @FormUrlEncoded
      Call<SubscriptionResponseModel> setSubs(@Field("pId") String pId,
                                              @Field("mNo") String mNo,
                                              @Field("status") String status);
      

      获取改造实例:

      private static Retrofit getSubsRetrofitInstance() {
          return new Retrofit.Builder()
                  .baseUrl(GlobalVar.BASE_URL) 
                  .addConverterFactory(GsonConverterFactory.create()).build();
      }
      

      getApiService

      public static ApiService getSubsApiService() {
          return getSubsRetrofitInstance().create(ApiService.class);
      }
      

      现在,调用函数:

      ApiService apiService = getSubsApiService();
      Call<SubscriptionResponseModel> subcriptionResponse = apiService.setSubs("1", "1", "y");
      
      subcriptionResponse.enqueue(new Callback<SubscriptionResponseModel>() {
          @Override
          public void onResponse(Call<SubscriptionResponseModel> call, Response<SubscriptionResponseModel> response) {
              if (response.isSuccessful()) {
                  /////print success message
              }
          }
      
          @Override
          public void onFailure(Call<SubscriptionResponseModel> call, Throwable t) {
              Log.d("error", t.getMessage());
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-24
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多