我知道类似的问题已经被提出了几次,但是我似乎找不到解决简单问题的方法:PATCH
动词。
所以我想问一下,在Android中使用OkHttp、Volley或Retrofit解决过PATCH
问题的人,请分享你的方法(如果你用了不同的方法或库,请也分享一下)。
我已经花费了三天的时间查找,但是我找不到任何解决这个问题的方法。我只是尝试通过RESTful API对由.NET环境构建和运行的表进行部分更新。
我目前正在使用Volley,但每次运行应用程序时,都会出现异常错误,显示类似于“PATCH is not an allowed...
”的信息,然后给出其他可以使用的动词数组,例如“POST、PUT、GET...”,这并没有什么帮助。
我已尝试像这样使用OkHttp
:
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());
}
});
但是client.newCall(request).enque(new Callback()...
会报错,提示request
不能应用于Request.Builder
。
顺便说一下,我是新手,对OkHttp不太了解。
无论如何,我已经努力许久仍然无法使PATCH
起作用。也许我在瞎猜,但如果有人有任何建议或思路可以让`PATCH`起作用,我将永远感激你。
感谢你的帮助。
.patch()
之后,您缺少对.build()
的调用。您需要一个Request
而不是一个Request.Builder
。 - Jesse Wilson