只允许一个HTTP方法。发现了GET和PUT。

huangapple 未分类评论49阅读模式
英文:

Only one HTTP method is allowed. Found GET and PUT

问题

java.lang.IllegalArgumentException: 只允许一个HTTP方法发现GET 和 PUT
        对于方法 ApiInterface.UpdateCoordinates
我已经尝试了最后2个小时来更新坐标但它不起作用一直抛出这个错误

    java.lang.IllegalArgumentException: 只允许一个HTTP方法发现GET 和 PUT
            对于方法 ApiInterface.UpdateCoordinates
            在 retrofit2.Utils.methodError(Utils.java:52)            在 retrofit2.Utils.methodError(Utils.java:42)            在 retrofit2.RequestFactory$Builder.parseHttpMethodAndPath(RequestFactory.java:251)            在 retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:224)            在 retrofit2.RequestFactory$Builder.build(RequestFactory.java:171)            在 retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)            在 retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)            在 retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)            在 retrofit2.Retrofit$1.invoke(Retrofit.java:149)            在 java.lang.reflect.Proxy.invoke(Proxy.java:1006)            在 $Proxy0.UpdateCoordinates(Unknown Source)            在 com.example.charlo.jkuat_mobile_app.util.LocationService$1.onLocationResult(LocationService.java:54)
>模型类
更新模型类
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class GpsUpdate {
    
        @SerializedName("success")
        @Expose
        private Boolean success;
        @SerializedName("data")
        @Expose
        private Data data;
        @SerializedName("message")
        @Expose
        private String message;
    
        public Boolean getSuccess() {
            return success;
        }
    
        public void setSuccess(Boolean success) {
            this.success = success;
        }
    
        public Data getData() {
            return data;
        }
    
        public void setData(Data data) {
            this.data = data;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    }

>模型类
数据模型类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Data {
        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("driverid")
        @Expose
        private Integer driverid;
        @SerializedName("companyid")
        @Expose
        private Integer companyid;
        @SerializedName("vehicleid")
        @Expose
        private Integer vehicleid;
        @SerializedName("warehouseid")
        @Expose
        private Integer warehouseid;
        @SerializedName("orders")
        @Expose
        private Integer orders;
        @SerializedName("status")
        @Expose
        private Integer status;
        @SerializedName("latitute")
        @Expose
        private String latitute;
        @SerializedName("longitude")
        @Expose
        private String longitude;
        @SerializedName("tripdate")
        @Expose
        private String tripdate;
        @SerializedName("created_at")
        @Expose
        private String createdAt;
        @SerializedName("updated_at")
        @Expose
        private String updatedAt;
    
    
        public Data(String latitute, String longitude) {
            this.latitute = latitute;
            this.longitude = longitude;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Integer getDriverid() {
            return driverid;
        }
    
        public void setDriverid(Integer driverid) {
            this.driverid = driverid;
        }
    
        public Integer getCompanyid() {
            return companyid;
        }
    
        public void setCompanyid(Integer companyid) {
            this.companyid = companyid;
        }
    
        public Integer getVehicleid() {
            return vehicleid;
        }
    
        public void setVehicleid(Integer vehicleid) {
            this.vehicleid = vehicleid;
        }
    
        public Integer getWarehouseid() {
            return warehouseid;
        }
    
        public void setWarehouseid(Integer warehouseid) {
            this.warehouseid = warehouseid;
        }
    
        public Integer getOrders() {
            return orders;
        }
    
        public void setOrders(Integer orders) {
            this.orders = orders;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        public String getLatitute() {
            return latitute;
        }
    
        public void setLatitute(String latitute) {
            this.latitute = latitute;
        }
    
        public String getLongitude() {
            return longitude;
        }
    
        public void setLongitude(String longitude) {
            this.longitude = longitude;
        }
    
        public String getTripdate() {
            return tripdate;
        }
    
        public void setTripdate(String tripdate) {
            this.tripdate = tripdate;
        }
    
        public String getCreatedAt() {
            return createdAt;
        }
    
        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }
    
        public String getUpdatedAt() {
            return updatedAt;
        }
    
        public void setUpdatedAt(String updatedAt) {
            this.updatedAt = updatedAt;
        }
    
    }

接口
接口类

    @PUT("update_driver/{dispatchid}?")
            Call<GpsUpdate> UpdateCoordinates(@Path("dispatchid") int id, @Field("latitude") String latitude, @Field("longitude") String longitude );

位置服务类
将坐标发送到后端的更新类

    @Override
        public void onCreate() {
            super.onCreate();
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
            tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
            service = ApiClient.createService(ApiInterface.class);
            locationCallback = new LocationCallback(){
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);
                    double lat = locationResult.getLastLocation().getLatitude();
                    double lng = locationResult.getLastLocation().getLongitude();
                    String latitude = String.valueOf(lat);
                    String longitude = String.valueOf(lng);
    
                    Data data = new Data(latitude,longitude);
    
                    Call<GpsUpdate> call = service.UpdateCoordinates(tokenManager.getToken().getDispatchid(),data.getLatitute(), data.getLongitude());
                    call.enqueue(new Callback<GpsUpdate>() {
                        @Override
                        public void onResponse(Call<GpsUpdate> call, Response<GpsUpdate> response) {
                            
                        }
    
                        @Override
                        public void onFailure(Call<GpsUpdate> call, Throwable t) {
    
                        }
                    });
英文:

java.lang.IllegalArgumentException: Only one HTTP method is allowed. Found: GET and PUT.
for method ApiInterface.UpdateCoordinates
i have tried for the last 2 hours to update the coordinates but it ain't working keeps throwing this error

java.lang.IllegalArgumentException: Only one HTTP method is allowed. Found: GET and PUT.
        for method ApiInterface.UpdateCoordinates
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.Utils.methodError(Utils.java:42)
        at retrofit2.RequestFactory$Builder.parseHttpMethodAndPath(RequestFactory.java:251)
        at retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:224)
        at retrofit2.RequestFactory$Builder.build(RequestFactory.java:171)
        at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:149)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy0.UpdateCoordinates(Unknown Source)
        at com.example.charlo.jkuat_mobile_app.util.LocationService$1.onLocationResult(LocationService.java:54)

>Model classes
the update model class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class GpsUpdate {

    @SerializedName(&quot;success&quot;)
    @Expose
    private Boolean success;
    @SerializedName(&quot;data&quot;)
    @Expose
    private Data data;
    @SerializedName(&quot;message&quot;)
    @Expose
    private String message;

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

>model class
the data model class

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {
    @SerializedName(&quot;id&quot;)
    @Expose
    private Integer id;
    @SerializedName(&quot;driverid&quot;)
    @Expose
    private Integer driverid;
    @SerializedName(&quot;companyid&quot;)
    @Expose
    private Integer companyid;
    @SerializedName(&quot;vehicleid&quot;)
    @Expose
    private Integer vehicleid;
    @SerializedName(&quot;warehouseid&quot;)
    @Expose
    private Integer warehouseid;
    @SerializedName(&quot;orders&quot;)
    @Expose
    private Integer orders;
    @SerializedName(&quot;status&quot;)
    @Expose
    private Integer status;
    @SerializedName(&quot;latitute&quot;)
    @Expose
    private String latitute;
    @SerializedName(&quot;longitude&quot;)
    @Expose
    private String longitude;
    @SerializedName(&quot;tripdate&quot;)
    @Expose
    private String tripdate;
    @SerializedName(&quot;created_at&quot;)
    @Expose
    private String createdAt;
    @SerializedName(&quot;updated_at&quot;)
    @Expose
    private String updatedAt;


    public Data(String latitute, String longitude) {
        this.latitute = latitute;
        this.longitude = longitude;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getDriverid() {
        return driverid;
    }

    public void setDriverid(Integer driverid) {
        this.driverid = driverid;
    }

    public Integer getCompanyid() {
        return companyid;
    }

    public void setCompanyid(Integer companyid) {
        this.companyid = companyid;
    }

    public Integer getVehicleid() {
        return vehicleid;
    }

    public void setVehicleid(Integer vehicleid) {
        this.vehicleid = vehicleid;
    }

    public Integer getWarehouseid() {
        return warehouseid;
    }

    public void setWarehouseid(Integer warehouseid) {
        this.warehouseid = warehouseid;
    }

    public Integer getOrders() {
        return orders;
    }

    public void setOrders(Integer orders) {
        this.orders = orders;
    }

    public Integer getStatus() {
        return status;
    }

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

    public String getLatitute() {
        return latitute;
    }

    public void setLatitute(String latitute) {
        this.latitute = latitute;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getTripdate() {
        return tripdate;
    }

    public void setTripdate(String tripdate) {
        this.tripdate = tripdate;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

}

Interface
interface class

@PUT(&quot;update_driver/{dispatchid}?&quot;)
        Call&lt;GpsUpdate&gt; UpdateCoordinates(@Path(&quot;dispatchid&quot;) int id, @Field(&quot;latitude&quot;) String latitude, @Field(&quot;longitude&quot;) String longitude );

location service class
the update class which sends the coordinates to the backend

@Override
    public void onCreate() {
        super.onCreate();
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        tokenManager = TokenManager.getInstance(getSharedPreferences(&quot;prefs&quot;, MODE_PRIVATE));
        service = ApiClient.createService(ApiInterface.class);
        locationCallback = new LocationCallback(){
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                double lat = locationResult.getLastLocation().getLatitude();
                double lng = locationResult.getLastLocation().getLongitude();
                String latitude = String.valueOf(lat);
                String longitude = String.valueOf(lng);

                Data data = new Data(latitude,longitude);

                Call&lt;GpsUpdate&gt; call = service.UpdateCoordinates(tokenManager.getToken().getDispatchid(),data.getLatitute(), data.getLongitude());
                call.enqueue(new Callback&lt;GpsUpdate&gt;() {
                    @Override
                    public void onResponse(Call&lt;GpsUpdate&gt; call, Response&lt;GpsUpdate&gt; response) {
                        
                    }

                    @Override
                    public void onFailure(Call&lt;GpsUpdate&gt; call, Throwable t) {

                    }
                });

答案1

得分: 0

我认为我应该将这标记为重复:

https://stackoverflow.com/questions/29556313/retrofit-how-fix-only-one-http-method-is-allowed-found-get-and-get

尝试将
@Path("dispatchid") int id
更改为
@Path("dispatchid") int dispatchid

英文:

I think I should have marked this as a duplicate:

https://stackoverflow.com/questions/29556313/retrofit-how-fix-only-one-http-method-is-allowed-found-get-and-get

Try changing the
@Path(&quot;dispatchid&quot;) int id
to
@Path(&quot;dispatchid&quot;) int dispatchid

huangapple
  • 本文由 发表于 2020年3月16日 02:10:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/60696085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定