我在Android中使用okHTTP来发出PUT请求。我添加了标题,并添加了.put请求。但不知何故,请求没有通过。我已经使用Log条目来跟踪它。代码如下:
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");
}
});
未提出请求。我不知道为什么。
答案 0 :(得分:0)
哦!不小心我在url端点之间放了一个空格!现在工作正常:)