【发布时间】:2016-11-08 14:52:18
【问题描述】:
我有这个方法可以向这个 URL 发出 PATCH 请求:
https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0
此方法的作用:它在 Microsoft Visual Studio - Team Foundation Server 中更新一个工作项的 System.Title,其 ID 为 8
public void testPostVolley(String url) {
/*Post data*/
JSONArray jsonBody = new JSONArray();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("op","replace");
jsonObject.put("path","/fields/System.Title");
jsonObject.put("value","Welcome to the jungle");
jsonBody.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("JSON Array " , String.valueOf(jsonBody));
JsonArrayRequest postRequest = new JsonArrayRequest(Request.Method.PATCH, url,
jsonBody,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("Response Post Request", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", MainRecyclerListView.TOKEN);
headers.put("Content-Type", "application/json-patch+json");
return headers;
}
};
queue.add(postRequest);
}
}
我在 POSTMAN 中尝试使用相同的标题和相同的正文,它可以工作,但在 Android 应用中我得到了以下响应:
07-06 13:05:49.253 2599-11381/ro.webivo.pockettask E/Volley:[209] BasicNetwork.performRequest:https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0 的意外响应代码 400
我还记录了我要发送的正文,是这个:
07-06 13:05:49.017 2599-2599/ro.webivo.pockettask D/JSON 数组:[{"op":"replace","path":"/fields/System.Title","value ":"欢迎来到丛林"}]
PATCH 请求的官方 API 位于此处:
https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items
你能帮帮我吗?这是 Volley 问题吗?
谢谢
【问题讨论】:
-
当你从 VSTS Rest API 收到“400 bad request”时,响应正文中应该有错误消息,你可以检查并分享正文消息吗?还有一个与 Android Volley 相关的类似问题:salesforce.stackexchange.com/questions/50382/…
-
VolleyError 响应中的错误信息是我给你的。你知道自定义错误响应的方法吗?
-
所以我
ve tried to change Request.Method to POST and add different headers like : headers.put("X-HTTP-Method-Override", "PATCH");, and this time I was getting some json back from VSTS Rest services telling me they accept only Content-Type of application/json-patch+json. Ive 改变了它,但我又得到了 400 的答案,没有别的。我试图记录error.networkresponse.data,但它为空。有什么想法吗?
标签: visual-studio post android-volley azure-devops patch