【问题标题】:BasicNetwork.performRequest: Unexpected response code 400 for "URL" - Volley PATCH RequestBasicNetwork.performRequest:“URL”的意外响应代码 400 - Volley PATCH 请求
【发布时间】: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


【解决方案1】:

经过两天的搜索,我找到了解决问题的方法。我只是改变了执行 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();
}

我希望这对其他人有帮助。

【讨论】:

  • 如果服务器不支持X-HTTP-Method-Override 怎么办?
猜你喜欢
  • 1970-01-01
  • 2019-08-06
  • 2015-01-03
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多