从Java连接PHP时出现IllegalStateException

时间:2018-01-28 15:28:21

标签: java php mysql database android-studio

我是java和Android Studio的初学者,我正坐在以下问题上:

我正在尝试将Java查询从Java应用程序发送到(工作)PHP脚本,该脚本执行收到的SELECT命令,应用程序读取选择的结果。

导致错误的原因是什么? :/ /提前感谢您的帮助。

我的错误日志:https://hastebin.com/goqabajuyo.sql PHP脚本:https://hastebin.com/otigopufeq.xml

 public void query(String query) throws IOException {
    try {
        URL url = new URL("http://localhost/db_query.php");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        OutputStream outputStream = conn.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String postData = URLEncoder.encode("mysql_query","UTF-8")+"="+URLEncoder.encode(query,"UTF-8");

        bufferedWriter.write(postData);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null){
            println(inputLine);
        }
        in.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

2 个答案:

答案 0 :(得分:0)

嗯,只是一个猜测:

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

答案 1 :(得分:0)

最后我得到了解决方案,只需将查询方法作为AsyncTask运行。

        AsyncTask.execute(new Runnable() {
        @Override
        public void run() {

        }
    });