2016-07-06 24 views
0

Ich habe diese Methode, die eine PATCH Anforderung an diese URL macht:BasicNetwork.performRequest: unerwartete Antwortcode 400 für "URL" - Volley PATCH anfordern

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

Was bedeutet diese Methode tun: Es aktualisiert eine workItem`s System.Title, die ID 8, in dem Microsoft Visual Studio hat - 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); 
} 

}

ich habe versucht, in POSTMAN w Ich habe dieselben Header und denselben Body und es funktioniert, aber in der Android App bekomme ich diese Antwort:

07-06 13: 05: 49.253 2599-11381/ro.webivo.pockettask E/Volley: [209] BasicNetwork.performRequest: Unexpected Antwortcode 400 für https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0

I protokolliert auch den Körper I`m Senden und ist dieses:

07-06 13: 05: 49,017 2599-2599/D ro.webivo.pockettask/JSON Array: [{"op": "ersetzen", "Pfad": "/ fields/System.Title", "Wert": "Willkommen im Dschungel"}]

Offizielle API für PATCH-Anfrage befindet sich hier:

https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items

Könnten Sie mir bitte helfen? Ist das ein Volley-Problem?

Danke

+0

Es sollte eine Fehlermeldung in der Antwort Nachricht geben, wenn Sie eine "400 ungültige Anfrage" von VSTS Rest API bekommen, können Sie überprüfen und die Nachricht teilen? Und eine ähnliche Frage im Zusammenhang mit Android Volley: http://salesforce.stackexchange.com/questions/50382/requestforupdate-always-returns-400 –

+0

Die Fehlermeldung in der Antwort VolleyError ist die, die ich Ihnen gab. Kennen Sie eine Möglichkeit, die Fehlerreaktion anzupassen? – dragosiv

+0

Also habe ich versucht, Request.Method in POST zu ändern und verschiedene Header wie: headers.put ("X-HTTP-Method-Override", "PATCH") hinzuzufügen; und dieses Mal habe ich ein JSON von VSTS bekommen Restdienste sagen mir, dass sie nur den Inhaltstyp der Anwendung/json-patch + json akzeptieren. Ich habe es geändert, aber ich bekomme wieder Antwort 400 und sonst nichts. Ich habe versucht, die error.networkresponse.data zu protokollieren, aber es kommt null. Irgendeine Idee? – dragosiv

Antwort

0

Nach zwei Tagen des Suchens ich eine Lösung für mein Problem gefunden. Ich habe gerade die Art geändert, wie ich die PATCH-Anfrage gemacht habe. Ich wechselte von Volley zu HttpClient und HttpCore Bibliotheken für diese Anfrage.

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(); 
} 

Ich hoffe, dass dies jemand anderem helfen wird.

+0

Was passiert, wenn ein Server 'X-HTTP-Method-Override' nicht unterstützt? –