Retrofit updating class PUT error code 301

724 views Asked by At

Maybe this is a silly question but I've been stucked on this for a while. I'm implementing some methods for a restApi. I'm using retrofit and I'm trying to update a client information. To do an update I'm using PUT method but I don't know why I'm always getting a 301 CODE and I can't update the information. Here is my code, thank you for everything.

public interface ClientInterface {

    @GET("/clients/{clientParam}")
    public Client fetchClient(@Path("clientParam") String client);

    @GET("/clients/")
    public void fetchAllClients(Callback<List<Client>> callback);

    @POST("/clients/")
    public void newClient(@Body Client client, Callback<Client> callback);

    @PUT("/clients/{clientParam}/")
    public void updateClient(@Path("clientParam") String cod, @Body Client client,Callback<Client> callback);
}

After that I use the update method in the following way

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Utils.getURLPreferences(DatosClienteActivity.this)).build();
ClientInterface clientRestInter = restAdapter.create(ClientInterface.class);
Client clientsUpdate = new Client();

    //Fetch data into clientsUpdate object

    clientRestInter.updateClient(codClient, clientsUpdate, new Callback<Client>() {

        @Override
        public void success(Client client, retrofit.client.Response response) {

        }

        @Override
        public void failure(RetrofitError error) {
            error.printStackTrace();
        }
    });

EDIT: If I use volley against the same URL it's working perfectly...

RequestQueue queue = Volley.newRequestQueue(this);

    final JSONObject jsonObject = new JSONObject();
    try {
        //Construct jsonObject
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest req = new JsonObjectRequest(Request.Method.PUT, URL, jsonObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        VolleyLog.v("Response:%n %s", response.toString(4));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
            error.printStackTrace();
        }
    });
    queue.add(req);
1

There are 1 answers

0
acostela On BEST ANSWER

Finally I solved it.

The problem was that we had two different Client objects and the one that you fetch is the one that you must change and use it to upload with PUT.

I hope that this help somebody.