使用翻新版发送POST正文参数

时间:2018-11-24 07:39:12

标签: java android json rest retrofit

我的api URL为:https://orderingspace.com/api/search 身体参数: 拉特:48.8834553 日志:2.3399433 英里:6

您能告诉我这段代码在哪里错误吗?

我的界面类:

  StringRequest stringRequest = new StringRequest(Request.Method.POST,
        WebConstants.BASE_URL + method,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
    @Override
    protected Map<String, String> getParams() {
      Map<String, String> map = new HashMap<>();
      map.put("firstname", "sandeep");
      map.put("lastname", "sada");
     map.put("gender", "d");
    map.put("country", "sdfasiio");
    map.put("zipcode", "test");
    map.put("email", "testmail@mail.com");
    map.put("password", "0");
    map.put("referral_code", "0");
    map.put("requested_plan", "0");

    Log.e("Map", map.toString());
            return map;
        }
};
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
        DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);

我的SearchData类(POJO):

public interface SearchAPI {
String BASE_URL = "https://orderingspace.com/api/";
@POST("search")
Call<SearchData> getSearchData(@Header("content-type") String contentType, @Body SearchDataRequest dataRequest);}

SearchDataRequest POJO类:

public class SearchData {

@SerializedName("status")
@Expose
public String status;
@SerializedName("message")
@Expose
public String message;
@SerializedName("result")
@Expose
public List<Result> result = null;
class Result {

    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("rname")
    @Expose
    public String rname;
    @SerializedName("email_address")
    @Expose
    public String emailAddress;
    @SerializedName("city")
    @Expose
    public String city;
    @SerializedName("address")
    @Expose
    public String address;
    @SerializedName("owner")
    @Expose
    public String owner;
    @SerializedName("user")
    @Expose
    public String user;
    @SerializedName("phone1")
    @Expose
    public String phone1;
    @SerializedName("phone2")
    @Expose
    public String phone2;
    @SerializedName("rest_images")
    @Expose
    public String restImages;
    @SerializedName("fax")
    @Expose
    public String fax;
    @SerializedName("status")
    @Expose
    public String status;
    @SerializedName("payment")
    @Expose
    public String payment;
    @SerializedName("acc_payment")
    @Expose
    public String accPayment;
    @SerializedName("delivery_type")
    @Expose
    public String deliveryType;
    @SerializedName("payment_process")
    @Expose
    public String paymentProcess;
    @SerializedName("delivery_time")
    @Expose
    public String deliveryTime;
    @SerializedName("delivery_area")
    @Expose
    public String deliveryArea;
    @SerializedName("receive_order")
    @Expose
    public String receiveOrder;
    @SerializedName("notify")
    @Expose
    public String notify;
    @SerializedName("email_radio")
    @Expose
    public String emailRadio;
    @SerializedName("email_image")
    @Expose
    public String emailImage;
    @SerializedName("email_text")
    @Expose
    public String emailText;
    @SerializedName("lat")
    @Expose
    public String lat;
    @SerializedName("log")
    @Expose
    public String log;
    @SerializedName("tax_rate")
    @Expose
    public String taxRate;
    @SerializedName("map_zones")
    @Expose
    public String mapZones;
    @SerializedName("hours")
    @Expose
    public String hours;
    @SerializedName("second_hours")
    @Expose
    public String secondHours;
    @SerializedName("more_hrs_radio")
    @Expose
    public String moreHrsRadio;
    @SerializedName("delivery_hrs_radio")
    @Expose
    public String deliveryHrsRadio;
    @SerializedName("delivery_hours")
    @Expose
    public String deliveryHours;
    @SerializedName("rdate")
    @Expose
    public String rdate;
    @SerializedName("vertices")
    @Expose
    public String vertices;
    @SerializedName("v_price")
    @Expose
    public String vPrice;
    @SerializedName("dely_type")
    @Expose
    public String delyType;
    @SerializedName("cuisine")
    @Expose
    public String cuisine;
    @SerializedName("partner")
    @Expose
    public String partner;
    @SerializedName("distance")
    @Expose
    public String distance;

    public String getId() {
        return id;
    }

    public String getRname() {
        return rname;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public String getCity() {
        return city;
    }

    public String getAddress() {
        return address;
    }

    public String getOwner() {
        return owner;
    }

    public String getUser() {
        return user;
    }

    public String getPhone1() {
        return phone1;
    }

    public String getPhone2() {
        return phone2;
    }

    public String getRestImages() {
        return restImages;
    }

    public String getFax() {
        return fax;
    }

    public String getStatus() {
        return status;
    }
    public String getPayment() {
        return payment;
    }
    public String getAccPayment() {
        return accPayment;
    }
    public String getDeliveryType() {
        return deliveryType;
    }

    public String getPaymentProcess() {
        return paymentProcess;
    }

    public String getDeliveryTime() {
        return deliveryTime;
    }

    public String getDeliveryArea() {
        return deliveryArea;
    }

    public String getReceiveOrder() {
        return receiveOrder;
    }

    public String getNotify() {
        return notify;
    }

    public String getEmailRadio() {
        return emailRadio;
    }

    public String getEmailImage() {
        return emailImage;
    }

    public String getEmailText() {
        return emailText;
    }

    public String getLat() {
        return lat;
    }

    public String getLog() {
        return log;
    }

    public String getTaxRate() {
        return taxRate;
    }

    public String getMapZones() {
        return mapZones;
    }

    public String getHours() {
        return hours;
    }

    public String getSecondHours() {
        return secondHours;
    }

    public String getMoreHrsRadio() {
        return moreHrsRadio;
    }

    public String getDeliveryHrsRadio() {
        return deliveryHrsRadio;
    }

    public String getDeliveryHours() {
        return deliveryHours;
    }

    public String getRdate() {
        return rdate;
    }

    public String getVertices() {
        return vertices;
    }

    public String getvPrice() {
        return vPrice;
    }

    public String getDelyType() {
        return delyType;
    }

    public String getCuisine() {
        return cuisine;
    }

    public String getPartner() {
        return partner;
    }

    public String getDistance() {
        return distance;
    }
}

public String getStatus() {
    return status;
}

public String getMessage() {
    return message;
}

public List<Result> getResult() {
    return result;
}}

MainActivity调用:

public class SearchDataRequest
{
private double lat;
private double log;
private int miles;

public double getLat() {
    return lat;
}

public SearchDataRequest(double lat, double log, int miles) {
    this.lat = lat;
    this.log = log;
    this.miles = miles;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLog() {
    return log;
}

public void setLog(double log) {
    this.log = log;
}

public int getMiles() {
    return miles;
}

public void setMiles(int miles) {
    this.miles = miles;
}}

翻新呼叫响应将失败响应发送到此处。请在此处建议如何发送带有正文参数的POST请求。

1 个答案:

答案 0 :(得分:0)

您需要更改界面代码

public interface SearchAPI {
    String BASE_URL = "https://orderingspace.com/api";
    @FormUrlEncoded
    @POST("./search")
    Call<SearchData> getSearchData(@Field("lat") String lat,
                                   @Field("log") String log,
                                   @Field("miles") String miles);}

和主要活动中

SearchAPI api = retrofit.create(SearchAPI.class);
    api.getSearchData(lat,log,miles).enqueue(new Callback<SearchData>() {
        @Override
        public void onResponse(Call<SearchData> call, Response<SearchData> response) {
            Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<SearchData> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show();
            System.out.println("Anshul : "+t.getMessage());
        }
    });