使用Retrofit 2进行POST请求发送对象

7

我很新于Retrofit库,我想知道使用Retrofit的post方法发送对象的最佳方式是什么。

以下是一些代码:

我的类:

public class ExampleClass implements Serializable {

    @SerializedName("id")
    int id;


    @SerializedName("name")
    String name;

    public ExampleClass(int id, String name) {
        this.id = id;
        this.name= name;
    }

}

我的界面:

public interface ApiInterface {
    @Headers("Content-Type: application/json")
    @POST("getclass/")
    Call<ExampleClass> getExampleClass(@Body ExampleClass exampleClass);
}

我和Retrofit的通话:

......
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<ExampleClass> call = apiService.getExampleClass(exampleClass);
        call.enqueue(new Callback<ExampleClass>() {
            @Override
            public void onResponse(Call<ExampleClass> call, Response<ExampleClass> response) {

                int statusCode = response.code();

                Log.i(TAG, "Status Code: " + statusCode);

            }

            @Override
            public void onFailure(Call<ExampleClass> call, Throwable t) {
                Log.i(TAG, "Error: " + t.toString());
            }

        });

但每次它都返回状态码500。

有什么方法可以用Retrofit发送对象吗?


1
请添加堆栈跟踪 - Matias Elorriaga
请附上您的adb日志。 - Anish Kumar Dubey
你是否已经将Gson或Jackson转换器添加到你的Retrofit实例中了? - nexus5x
我还向我的Retrofit实例添加了Gson转换器... - Marco Ferretti
3个回答

3
您可以通过 @FormUrlEncoded 发送数据,例如:
@FormUrlEncoded
@Headers("Content-Type: application/json")
@POST("getclass/")
Call<ExampleClass> getExampleClass(@Field("id") int id, @Field("name") String name);

但我认为你的方式是最简单且正确的。

如果你不懒,你可以在这里找到关于Retrofit的所有内容。


1

500内部服务器错误

500状态码或内部服务器错误意味着服务器由于未知原因无法处理请求。有时,当更具体的5xx错误更合适时,会出现此代码。

这个错误最常见的原因是服务器配置错误(例如格式不正确的.htaccess文件)或缺少软件包(例如尝试在没有正确安装PHP的情况下执行PHP文件)。

您可以查看它。对我来说工作得很好

Api InterFace类

public interface ApiInterface {

    @FormUrlEncoded
        @POST("/billingrest/save")
        Call<LoginError> sendProductList(@Body SaveBill saveBill);

}

SaveBill 模型

package com.example.dev.billingsoftware.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

/**
 * Created by dev on 4/8/17.
 */
public class SaveBill {

    @SerializedName("customer_name")
    @Expose
    private String customerName;
    @SerializedName("customer_id")
    @Expose
    private String customerId;
    @SerializedName("bill_number")
    @Expose
    private String billNumber;
    @SerializedName("mobile_number")
    @Expose
    private String mobileNumber;
    @SerializedName("bill_date")
    @Expose
    private String billDate;
    @SerializedName("address")
    @Expose
    private String address;
    @SerializedName("count")
    @Expose
    private String count;
    @SerializedName("data")
    @Expose
    private List<BillData> data = null;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getBillNumber() {
        return billNumber;
    }

    public void setBillNumber(String billNumber) {
        this.billNumber = billNumber;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public String getBillDate() {
        return billDate;
    }

    public void setBillDate(String billDate) {
        this.billDate = billDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

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

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

}

0
  1. 你的 API 服务器是否正常工作?

    HTTP 状态码 500 表示内部服务器错误。首先使用可靠的客户端测试你的 API 服务器。查看服务器上的错误日志,确定哪一方面引起了问题。

  2. 你是否在 Retrofit 实例中添加了 Gson 或 Jackson 转换器?

    如果你的服务器没有问题,那么客户端(即你的应用程序)可能存在问题。一个格式不正确的请求体可能导致服务器错误。

    你是否添加了 Gson(或 Jackson)转换器?你的 @Headers 注解不会自动将对象转换为 json。如果你的服务器期望请求的是 json,那么你发送的不是 'object',而是对象的 json 表示形式的字符串。实现 Serializable 接口是不必要的。如果你添加了 Gson 转换器,它会自动将任意对象转换为 json,否则你必须使用 RequestBody

    来自 Retrofit github 页面(REQUEST BODY 部分)

    如果没有添加转换器,则只能使用 RequestBody


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接