PATCH Verb on Android (OkHttp, Volley, Retrofit ...)

I know that a similar question has been asked several times, but I can not find a solution for a simple problem: PATCHverb.

Therefore, I ask anyone who has solved the PATCH problem in Android either using OkHttp, Volley or Retrofit (or if you used another method or library, please share).

I spent 3 days searching, and I can not find a solution to this problem. All I am trying to do is to partially update the table through the RESTful API, which is built and running in the .NET environment.

I am currently using Volley, but every time I run the application, I get an exception error that says something along the lines PATCH is not an allowed...?, and then gives an array of other verbs that can be used, such as "POST", PUT, GET. .. ". So, not very useful at all.

I tried using OkHttpas follows:

  JSONObject infoRequestBody = new JSONObject();


    try {


        infoRequestBody.put("Salutation", "MRrr.");
        infoRequestBody.put("FirstName", "Work Now!!");
        infoRequestBody.put("LastName", "Test from my mobile again!! Okhttp");
        infoRequestBody.put("MiddleName", "From the Mobile");
        infoRequestBody.put("NickName", null);
        infoRequestBody.put("BirthDate", "1978-11-23T00:00:00.000Z");
        infoRequestBody.put("CompanyName", null);
        // infoRequestBody.put("RegDate", "2015-01-18T23:39:13.873Z");
        infoRequestBody.put("Gender", "Male");
        infoRequestBody.put("IsPregnant", null);
        infoRequestBody.put("IsNursing", null);
        infoRequestBody.put("WorkPhone", null);
        infoRequestBody.put("WorkPhoneExt", null);
        infoRequestBody.put("PhoneNumber", null);
        infoRequestBody.put("Address", "My android app working!!");
        infoRequestBody.put("Address2", null);
        infoRequestBody.put("City", null);
        infoRequestBody.put("State", "CA");
        infoRequestBody.put("ZipCode", null);
        infoRequestBody.put("EmailAddress", "email@me.com");
        infoRequestBody.put("Occupation", null);
        infoRequestBody.put("CountryofResidence", "United States");
        infoRequestBody.put("CountryCode", "US");
        infoRequestBody.put("Ethnicity", "Asian");
        infoRequestBody.put("ModifiedDate", "2015-01-18T23:39:13.873Z");
        infoRequestBody.put("ModifiedBy", null);
        infoRequestBody.put("AcceptedTerms", true);
        infoRequestBody.put("AcceptedPrivacyPolicyCheckbox", false);
        infoRequestBody.put("AcceptedUnder18ConsentCheckbox", false);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
  RequestBody body = com.squareup.okhttp.RequestBody.create(JSON, infoRequestBody.toString());


    com.squareup.okhttp.Request.Builder request = new com.squareup.okhttp.Request.Builder()
             .url("APIUrls.PATIENT_INFO_URL")
            .addHeader("Content-Type", "application/json")
            .addHeader("Accept", "application/json")
            .addHeader("X-ZUMO-APPLICATION", APIUrls.ZUMO_KEY)
            .patch(body);



    client.newCall(request).enqueue(new Callback() {




        @Override
        public void onFailure(com.squareup.okhttp.Request request, IOException e) {


        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {

            if (!response.isSuccessful()) throw new IOException("Unexpected code" + response);

            Headers responseHeaders = response.headers();
            for (int i = 0; i < responseHeaders.size(); i++) {
                System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
            }


            System.out.println(response.body().string());

        }
    });

but client.newCall(request).enque(new Callback()...gives me an error saying that it requestcannot be applied to Request.Builder.

I'm new to OkHttp, by the way.

In any case, I try my best to PATCHwork too long. Maybe I'm shooting in the dark, but if anyone has suggestions, thoughts on how to make PATCH work, I will be forever grateful to you.

Thank you for your help.

+4
3

! @Adam , , . . , ( , @Adam , , , !):

-,

UserUpdate :

public class UserUpdate {
public String Salutation;
public String FirstName;
public String LastName;
public String MiddleName;
public String NickName;

     //Setters and Getters...
  public String getSalutation() {
      return Salutation;
  }

  public void setSalutation(String salutation) {
      Salutation = salutation;
  }

  public String getFirstName() {
     return FirstName;
  }

  public void setFirstName(String firstName) {
     FirstName = firstName;
  }

  public String getLastName() {
      return LastName;
  }

  public void setLastName(String lastName) {
      LastName = lastName;
  }

public String getMiddleName() {
    return MiddleName;
}

public void setMiddleName(String middleName) {
    MiddleName = middleName;
}

public String getNickName() {
    return NickName;
}

 public void setNickName(String nickName) {
     NickName = nickName;
 }


}

interface class, PatientInfoAPI:

public interface PatientInfoAPI{

@PATCH("/tables/PatientInformation/{id}")
  public void update(@Body UserUpdate updatedDAta, @Path("id") String  id,Callback<UserUpdate> response);


 }

, "PATCH'd", , , @Body, ( )

, , :

 RestAdapter restAdapter = new RestAdapter.Builder()
            .setClient(new OkClient()) //very important!!!
            .setEndpoint(ENDPOINT)//base url
            .setRequestInterceptor(new RequestInterceptor() {//You may not need to pass headers, but I do, so this is how it done.
                @Override
                public void intercept(RequestFacade request) {

                    request.addHeader("Content-Type", "application/json");
                    request.addHeader("Accept", "application/json");
                    request.addHeader("X-ZUMO-APPLICATION", APIUrls.ZUMO_KEY);

                }
            })
            .build();
    PatientInfoAPI patchService = restAdapter.create(PatientInfoAPI.class);

    UserUpdate updatedUser = new UserUpdate();
    updatedUser.setSalutation("Sr.");
    updatedUser.setFirstName("YesSirtItsWorking");
    updatedUser.setLastName("Bays");
    updatedUser.setMiddleName("ComeOn Now man!");
    updatedUser.setNickName("");

     //Passing the tableId along with the data to update
     patchService.update(updatedUser, APIUrls.TABLE_ID, new retrofit.Callback<UserUpdate>() {
        @Override
        public void success(UserUpdate userUpdates, retrofit.client.Response response) {


            Log.v("All is good", "Nickname:" +  userUpdates.getNickName().toString() + " Email: " + userUpdates.getEmailAddress().toString());



        }

        @Override
        public void failure(RetrofitError error) {

            Log.v("All is good", error.getLocalizedMessage().toString());


        }
    });

, , , , , Retrofit OkHttp, , , PATCH Android, . , @Adam . @Adam , - , . , @Adam!!! !

+1

GreyBeardedGeek, Retrofit + OkHttp, .

RestAdapter restAdapter = new RestAdapter.Builder()
                .setClient(new OkClient())
                .setEndpoint(ENDPOINT)
                .build();
MyPatchService patchService = restAdapter.create(MyPatchService.class);
UserUpdate updatedUser = new UserUpdate();
updatedUser.Salutation = "Mr";
updatedUser.FirstName = "Adam";
patchService.update(updatedUser);

MyPatchService :

public interface MyPatchService {
    @PATCH("/some/update/endpoint")
    User update(UserUpdate updatedObject);
}

UserUpdate :

public class UserUpdate {
    public String Salutation;
    public String FirstName;
    public String LastName;
}

, JSON, , ( , LastName , - Gson ):

{
  "Salutation": "Mr",
  "FirstName": "Adam"
}

request/response - Java- , / . , vars Integer, Boolean .., , (.. null).

+4
+1
source

All Articles