BasicNetwork.performRequest:“URL”的意外响应代码400 - Volley PATCH Request

时间:2016-07-06 14:01:20

标签: visual-studio post android-volley azure-devops patch

我有这个方法对这个URL发出PATCH请求:

https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0

这个方法做了什么:它更新了一个workItem的System.Title,它具有id 8,在Microsoft Visual Studio中 - Team Foundation Server

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中使用相同的Headers和相同的Body并且它可以正常工作,但是在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

你能帮帮我吗?这是排球问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

经过两天的搜索,我找到了解决问题的方法。我只是改变了我做PATCH请求的方式。我从Volley更改为HttpClient和HttpCore库以获取此请求。

JSONArray jsonBody = new JSONArray();
JSONObject jsonObject = new JSONObject();

try {
    jsonObject.put("op", "replace");
    jsonObject.put("path", "/fields/System.Title");
    jsonObject.put("value", title);
    jsonBody.put(jsonObject);
} catch (JSONException e) {
    e.printStackTrace();
}

HttpClient hc = new DefaultHttpClient();
String message;

HttpPost updateResultsPost = new HttpPost(url);


try {
    message = jsonBody.toString();


    updateResultsPost.setEntity(new StringEntity(message, "UTF8"));
    updateResultsPost.setHeader("Authorization", MainRecyclerListView.TOKEN);
    updateResultsPost.setHeader("X-HTTP-Method-Override", "PATCH");
    updateResultsPost.setHeader("Content-type", "application/json-patch+json");
    HttpResponse resp = hc.execute(updateResultsPost);
    if (resp != null && resp.getStatusLine().getStatusCode() == 200) {
        String responseBody = EntityUtils.toString(resp.getEntity());
        JSONObject jsonUpdatedResponse = new JSONObject(responseBody);
        Log.d("Status line", "" + String.valueOf(jsonUpdatedResponse));

    }

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}

我希望这会帮助别人。