当前位置: 首页 > 知识库问答 >
问题:

使用修改发送POST体参数

段曦
2023-03-14

我有一个api URL:https://orderingspace.com/api/search Body parameter:lat:48.8834553 log:2.3399433 miles:6

public interface SearchAPI {
String BASE_URL = "https://orderingspace.com/api/";
@POST("search")
Call<SearchData> getSearchData(@Header("content-type") String contentType, @Body SearchDataRequest dataRequest);}
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;
}}
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;
}}
private void getSearchDataFromAPI()
{
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(SearchAPI.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
            .build();


    SearchAPI api = retrofit.create(SearchAPI.class);
    SearchDataRequest searchDataRequest = new SearchDataRequest(48.8834553,2.3399433,6);

    api.getSearchData("application/x-www-form-urlencoded",searchDataRequest).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());
        }
    });
}

共有1个答案

冯奇思
2023-03-14

您需要更改接口代码

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());
        }
    });
 类似资料:
  • 单击from-data或x-www-form-urlencoded 然后在键/值字段上传递两个参数。 ANDROID改装设置 原因是我使用了2个API(webapi和wcf)。我的所有其他响应都是对象数组。[{},{}]但在此呼叫中,我收到了以下信息 但我还是无法解析响应。

  • 我需要执行修改后的请求,但我有一个问题,我不能很好地理解。在尝试使用代码之前,我用Postman和request测试了api调用,如下所示: 下面是我的android代码: 编辑请求: null

  • 我正在构建一个简单的web应用程序,并尝试创建一个登录页面。该页面由一个带有加载servlet的表单的JSP组成。 在servlet中: 这段代码可以工作,但它在URL字符串中包含了用户名和密码,所以这显然不是一个好的做法。我尝试使用POST来实现这一点,但我得到了一个错误。(HTTP状态405-此URL不支持HTTP方法POST) 我需要知道如何使用POST将参数从JSP发送到Servlet。我

  • 我有一个对象,作为另一个GET请求的响应接收它<代码>jsonElement。toString() 看起来像JSON数组: 我需要通过另一个使用改装的POST请求发送此字符串。如何通过POST请求发送jsonElement或String对象?我的方法声明应该怎么看?例如:

  • 我正在尝试使用Chrome扩展Postman测试一个简单的PHP页面。当我发送URL参数时,脚本运行良好(例如变量在参数中可用)。当我将它们作为参数发送时,参数仅包含。 剧本: 我错过了什么?

  • 问题内容: 我想使用JavaScript中的XMLHttpRequest发送一些数据。 说我的HTML形式如下: 如何在JavaScript中使用XMLHttpRequest编写等效项? 问题答案: 下面的代码演示了如何执行此操作。