接收" 501未实施"使用HttpURLConnection时

时间:2015-09-25 22:11:54

标签: android network-programming httpurlconnection

我的项目针对的是SDK 23,从现在开始,据我所知,我必须使用HttpURLConnection代替HttpClient来发帖请求,但我不断获得{{1来自我的webservice的响应代码(我很确定它完全正常,我毫不怀疑)在发出请求以将用户存储在远程mysql数据库中时。以下是主要的连接代码:

501

在我的片段中,我打网络电话:

class DatabaseConnector extends AsyncTask<String, Void, String> {
    private final String CONNECTION_URL = "webservice_address";

    @Override
    protected String doInBackground(String... params) {
        String response = "initial value";

        try {
            URL url = new URL(CONNECTION_URL);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setReadTimeout(10000);
            con.setConnectTimeout(15000);
            con.setRequestMethod("POST");

            JSONObject json = new JSONObject();

            json.put("query", params[0]);

            JSONObject conn = new JSONObject();
            conn.put("database", "database");
            conn.put("hostname", "hostname");
            conn.put("password", "password");
            conn.put("username", "username");
            conn.put("port", "port");

            json.put("conn", conn.toString());

            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
            wr.write(json.toString());
            wr.flush();
            wr.close();

            con.connect();

            //display what returns the POST request

            StringBuilder sb = new StringBuilder();
            int HttpResult = con.getResponseCode();

            System.out.println("Response CODE: "+ con.getResponseCode());

            if (HttpResult == HttpURLConnection.HTTP_OK) {
                System.out.println("Passed1");
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));

                while ((response = br.readLine()) != null) {
                    sb.append(response + "\n");
                }

                br.close();

                System.out.println("E: " + sb.toString());

                response = sb.toString();
            } else {
                System.out.println("E2: "+con.getResponseMessage());
                response = "Error1: "+con.getResponseMessage();
            }
        } catch (Exception e) {
            response = "Error2: "+e.toString();
        }

        return response;
    }
}

没关系错误的sql语句,它的意思就是这样,我希望webservice能够回显那个语法错误。我尝试过很多东西,代码和示例,但它似乎没有用。

重点是:我不断收到错误代码 501 Not Implemented 。为什么?我该如何解决这个问题?

修改

关于如何改进我在Android上进行网络呼叫的方式的任何提示或评论都是值得欢迎的,我对此非常陌生。我正在使用异步调用但不知何故它仍然在主线程中执行,导致调试器抱怨:

try {
    String result = new DatabaseConnector().execute("SELECssT 1").get();
    Toast.makeText(ContentActivity.this, "Result: "+result, Toast.LENGTH_LONG).show();
} catch (Exception ex) {
    Toast.makeText(ContentActivity.this, "Error3: "+ex.toString(), Toast.LENGTH_LONG).show();
}

谢谢大家。

0 个答案:

没有答案