1
我在Android中使用okHTTP來發出PUT請求。我添加了頭文件並添加了.put請求。但不知何故,請求沒有通過。我已經使用日誌條目來跟蹤它。代碼如下所示:PUT失敗OKHTTP
String url =「http://xxxxxxxxxx.com/v1.0/xxxxxx/」+ xxxxxxx;
JSONObject jSon = new JSONObject();
try {
jSon.put("prescription_interval_id", prescriptionIntervalId);
} catch (JSONException e) {
e.printStackTrace();
}
try {
jSon.put("prescription_auto_refill", false);
} catch (JSONException e) {
e.printStackTrace();
}
String data = jSon.toString();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, data);
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", token)
.addHeader("Content-Type", "application/json")
.put(body)
.build();
//
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d("FAIL","CALL FAILED");
Log.d("Request",request.toString());
}
@Override
public void onResponse(Response response) throws IOException {
Log.d("Response",response.toString());
Log.d("SUCCESS","CALL SUCCEEDED");
}
});
該請求沒有被提出。我不知道爲什麼。
您是否嘗試過調用'client.newCall(request).execute()'而不是'enqueue()'來查看連接是否正確? –
檢查響應代碼以查看與請求失敗的內容可能也很有幫助。 – NoChinDeluxe
protocol = http/1.1,code = 400,message = Bad Request – Ackman