改造发送Json

时间:2018-04-25 16:48:36

标签: android retrofit retrofit2

我需要通过Retrofit将Json发送到支付网关。我真的不知道我的邮差很严重,一切运作良好。当我试图通过改造来适应时,我只会得到null作为回应。

我的课程实用工具

 public class ApiUtils {
        private ApiUtils() {}

 public static final String BASE_URL = "https://api.payulatam.com/";

        public static APIService getAPIService() {

            return RetrofitClient.getClient(BASE_URL).create(APIService.class);
        }
    }

我的界面类

public interface APIService {

    @Headers({
            "Accept: application/json",
            "Content-Type: application/json"
    })
    @POST("payments-api/4.0/service.cgi/")
    Call<Ping> sendPing(@Body JsonObject jsonObject);

}

我的POJO服务器响应。

public class Ping {

    @SerializedName("code")
    @Expose
    private String code;
    @SerializedName("error")
    @Expose
    private Object error;
    @SerializedName("transactionResponse")
    @Expose
    private Object transactionResponse;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Object getError() {
        return error;
    }

    public void setError(Object error) {
        this.error = error;
    }

    public Object getTransactionResponse() {
        return transactionResponse;
    }

    public void setTransactionResponse(Object transactionResponse) {
        this.transactionResponse = transactionResponse;
    }

}

主要活动,Ping是我使用网关请求的要求发送Json的方法。我总是得到一个答案无效

public class MainActivity extends AppCompatActivity {

    private JsonObject SendObj;
    private JsonObject objPing;
    private TextView mResponseTv;
    private APIService mAPIService;
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button submitBtn = (Button) findViewById(R.id.btn_submit);

        mAPIService = ApiUtils.getAPIService();

        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Ping();
            }
        });
    }

    private void Ping(){
        JsonObject objMerchant = new JsonObject();
        objMerchant.addProperty("apiLogin", "XXXXXXXXXXX");
        objMerchant.addProperty("apiKey","XXXXXXXXXXXXXX");

        JsonObject objPing = new JsonObject();
        objPing.addProperty("test", "false");
        objPing.addProperty("language","es");
        objPing.addProperty("command","PING");
        objPing.add("merchant",objMerchant);
        SendPing(objPing);
        Log.i(TAG, "Obj" + objPing.toString());

    }


    public void SendPing(JsonObject body) {
        Call <Ping> call = mAPIService.sendPing(body);
        call.enqueue(new Callback<Ping>() {
            @Override
            public void onResponse(Call<Ping> call, Response<Ping> response) {
                Log.d(TAG,"Getting response from server : "+ response.body());
            }

            @Override
            public void onFailure(Call<Ping> call, Throwable t) {
                Log.d(TAG,"Getting response from server failure: "+ t);
            }
        });
    }




}

postman request

2 个答案:

答案 0 :(得分:0)

为什么@POST中没有端点(&#34; ./")?

BASE_URL基本上应该是 https://api.payulatam.com/ ,你的POST端点应该是

@POST("payments-api/4.0/service.cgi/")

此外,为什么要手动创建请求正文?您可以使用POJO类来序列化请求,我认为处理这种方式会更容易。 例如,

public class Request{ @SerializedName("language") public String language; //you can use private as well }

并创建请求,

Request request = new Request(); request.language = "es";

所以在你的界面中,这样说吧:

Call<Ping> sendPing(@Body Request request);

你能试试看吗?

邮递员请求更新了请求对象:

public class Request {@SerializedName("test")
    public boolean test;

    @SerializedName("language")
    public String language;

    @SerializedName("command")
    public String command;

    @SerializedName("merchant")
    public Merchant merchant;

    public class Merchant{
        @SerializedName("apiLogin")
        public String apiLogin;

        @SerializedName("apiKey")
        public String apiKey;
    }
}

答案 1 :(得分:0)

你不需要提到:

@Headers({
        "Accept: application/json",
        "Content-Type: application/json"
})

请从代码中删除它。

test在你的邮递员请求中是boolean但是你在这里传递为字符串,所以改变如下:

 JsonObject objMerchant = new JsonObject();
    objMerchant.addProperty("apiLogin", "XXXXXXXXXXX");
    objMerchant.addProperty("apiKey","XXXXXXXXXXXXXX");

    JsonObject objPing = new JsonObject();
    objPing.addProperty("test", "false");
    objPing.addProperty("language","es");
    objPing.addProperty("command","PING");
    objPing.add("merchant",objMerchant);

为:

 JsonObject objMerchant = new JsonObject();
    objMerchant.addProperty("apiLogin", "XXXXXXXXXXX");
    objMerchant.addProperty("apiKey","XXXXXXXXXXXXXX");

    JsonObject objPing = new JsonObject();
    objPing.addProperty("test", false);
    objPing.addProperty("language","es");
    objPing.addProperty("command","PING");
    objPing.add("merchant",objMerchant);

测试属性在你的邮递员请求中是布尔值,但是你在这里作为字符串传递。