HttpUrlConnection:狀態405不允許使用錯誤方法。HttpUrlConnection:不允許使用狀態405錯誤方法
當發送請求時,我收到來自服務器405的錯誤。我做錯了什麼? 發送請求時,我收到來自服務器405的錯誤。我做錯了什麼?
class SendAnswer extends AsyncTask<Void, String, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
try {
URL url = new URL(AppConstant.SEND_ANSWER + _survey_id + "/answers/");
System.out.println("SEND URL " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches (false);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Auth-Token", mSettings.getString("token", ""));
connection.setRequestProperty("Content-Type","application/json");
connection.connect();
JSONObject params = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject answerObj = new JSONObject();
JSONArray survey_pages = new JSONArray();
JSONObject survey_page = new JSONObject();
JSONObject surveyObj = new JSONObject();
JSONObject jObj = new JSONObject();
survey_page.put("id", _page_id);
survey_pages.put(survey_page);
surveyObj.put("survey_pages", survey_pages);
int listSize = question_list.size();
for (int i = 0; i<listSize; i++) {
question = question_list.get(i).id;
params.put("question", question);
params.put("answer", "true");
jsonArray.put(params);
jObj.put("answers", jsonArray);
}
survey_pages.put(jObj);
System.out.println("jsonString: "+ surveyObj.toString());
DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
printout.writeBytes(surveyObj.toString());
printout.flush();
printout.close();
int status = connection.getResponseCode();
System.out.println("Connection " + status);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
你到一個特定的執行POST鏈接。很可能您發佈的鏈接不允許發佈POST,只有GET。 – Fortega