[英]Sending POST Request With Parameters Using Retrofit
I want to send POST request to a server and receive some data in JSON format But it won't work. 我想将POST请求发送到服务器并接收JSON格式的一些数据,但是它不起作用。
Am I missing something? 我想念什么吗?
My interface class: 我的界面类:
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);
}
in MainActivity I Use Retrofit Like This: 在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());
}
});
Class For receive Data : 用于接收数据的类:
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;
}
}
I tested my Api in Postman and reqbin.com and I get desire response but when I send POST request in Android I get the following Error: 我在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
POSTMAN Result : 邮递员结果:
{
"status": "Success",
"orderHash": "sdfknsjkdnf",
"paymentInfo": {
"url": "some url"
}
}
And something weird is when I send incomplete necessary parameters (For Example "type") I get the expected response in case of incomplete parameters But when I send all parameters It won't work. 奇怪的是,当我发送不完整的必要参数(例如“类型”)时,如果参数不完整,则会得到预期的响应,但是当我发送所有参数时,它将无法正常工作。 I saw almost every question about This Topic in stackoverflow but none of them helped me.
我在stackoverflow中几乎看到了有关此主题的所有问题,但没有一个问题对我有帮助。
Use this model instead
改用这个模型
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+"]";
}
}
Payment modal
付款方式
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+"]";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.