使用翻新版发送带有参数的POST请求

时间:2019-07-26 14:46:18

标签: android post retrofit

我想向服务器发送POST请求并接收JSON格式的一些数据,但是它不起作用。

我想念什么吗?

我的界面类:

public interface JsaonPlaceHolder {
@POST("topup")
@FormUrlEncoded
Call<Post> createPost(@Field("type") String type,
                   @Field("amount") String amount,
                   @Field("cellphone") String cellphone,
                   @Field("email") String email,
                   @Field("webserviceId") String webserviceId,
                   @Field("redirectUrl") String redirectUrl,
                   @Field("issuer") String issuer,
                   @Field("redirectToPage") String redirectToPage,
                   @Field("scriptVersion") String scriptVersion,
                   @Field("firstOutputType") String firstOutputType,
                   @Field("secondOutputType") String secondOutputType);
}

在MainActivity中,我使用这样的改造:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("something/EasyCharge/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    JsaonPlaceHolder jsaonPlaceHolder = 
 retrofit.create(JsaonPlaceHolder.class);


    Call<Post> call = jsaonPlaceHolder.createPost("MTN", "1000", 
"*********", "hossein_abedi@outlook.com", "*************", "", " ", "", 
"Android", "json", "json");

    call.enqueue(new Callback<Post>() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {

            if (!response.isSuccessful()) {
                textviewResult.setText("code" + response.code());
                return;
            }
            Post post1 = response.body();

            String content = post1.getErrorMessage();

            textviewResult.setText(content);

        }

        @Override
        public void onFailure(Call<Post> call, Throwable t) {

            textviewResult.setText(t.toString());

        }
    });

用于接收数据的类:

public class Post {
private String  status;

private String  orderHash ;

private String  paymentInfo;

private String  errorMessage;



public String getStatus() {
    return status;
}

public String getOrderHash() {
    return orderHash;
}

public String getPaymentInfo() {
    return paymentInfo;
}

public String getErrorMessage() {
    return errorMessage;
}
}

我在Postman和reqbin.com中测试了我的Api,并且得到了欲望响应,但是当我在Android中发送POST请求时,出现以下错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 
Expected a string but was BEGIN_OBJECT at line 1 column 63 path 
$.paymentInfo

邮递员结果:

{
  "status": "Success",
  "orderHash": "sdfknsjkdnf",
  "paymentInfo": {
    "url": "some url"
}
}

奇怪的是,当我发送不完整的必要参数(例如“类型”)时,如果参数不完整,我会得到预期的响应,但是当我发送所有参数时,它将无法正常工作。 我在stackoverflow中几乎看到了有关此主题的所有问题,但没有一个问题对我有帮助。

1 个答案:

答案 0 :(得分:0)

  

改为使用此模型

public class MyPojo
    {
        private PaymentInfo paymentInfo;

        private String status;

        private String orderHash;

        public PaymentInfo getPaymentInfo ()
        {
            return paymentInfo;
        }

        public void setPaymentInfo (PaymentInfo paymentInfo)
        {
            this.paymentInfo = paymentInfo;
        }

        public String getStatus ()
        {
            return status;
        }

        public void setStatus (String status)
        {
            this.status = status;
        }

        public String getOrderHash ()
        {
            return orderHash;
        }

        public void setOrderHash (String orderHash)
        {
            this.orderHash = orderHash;
        }

        @Override
        public String toString()
        {
            return "ClassPojo [paymentInfo = "+paymentInfo+", status = "+status+", orderHash = "+orderHash+"]";
        }
    }
  

付款方式

public class PaymentInfo
{
    private String url;

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [url = "+url+"]";
    }
}