HttpUrlConnection:不允许状态405错误方法

时间:2014-03-28 10:18:01

标签: android

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;

        }
    }

2 个答案:

答案 0 :(得分:0)

试试这个

      HttpClient client = getHttpClient();
      HttpPost request = new HttpPost(universal_URL_MENU+"?request_menu="+start_menu);

      HttpResponse response = client.execute(request); 
            System.out.println("response in class"+response);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

其中getHttpClient()

             private static HttpClient getHttpClient() {
        if (mHttpClient == null) {
            mHttpClient = new DefaultHttpClient();
            final HttpParams params = mHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
        }
        return mHttpClient;     
    }

////

    private static HttpClient mHttpClient;            

答案 1 :(得分:0)

                conn = (HttpURLConnection) new URL(url).openConnection();
                conn.setRequestMethod(reqMethod);
                conn.setConnectTimeout(15000);
                conn.setReadTimeout(15000);
                byte[] data = params.getBytes();
                if(!isJoinOrLoginOrResetPasswordMethod){
                    conn.setRequestProperty("Authorization",USE_KEY);
                }
                conn.setRequestProperty("X-Application-Id", APP_ID);

                // conn.setRequestProperty("X-Application-Id"

                conn.setDoOutput(true);
                // conn.setDoInput(true);
                OutputStream out = conn.getOutputStream();
                // out.write(params.getBytes("UTF-8"));
                out.write(data);
                out.flush();
                out.close();
                int responseCode = conn.getResponseCode();

                 The responseCode =405   what is wrong ??