duanpao4172 2016-09-15 15:20
浏览 135
已采纳

HttpURLConnection:结果json-code建议页面不存在(404),即使url是正确的

I am trying to get data from an MySQL database using a php-file. My java code is as follows:

    HttpURLConnection conn = null;
    URL url = null;

    try {

        url = new URL(getURL);
        System.out.println(getURL);
        conn = (HttpURLConnection)url.openConnection();

        //conn.setReadTimeout(READ_TIMEOUT);
        //conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod("POST");

        // setDoInput and setDoOutput method depict handling of both send and receive
        conn.setDoInput(true);
        conn.setDoOutput(true);

        // Append parameters to URL
        Uri.Builder builder = new Uri.Builder();
        builder.appendQueryParameter("user", USER);
        builder.appendQueryParameter("pass", PASS);
        builder.appendQueryParameter("server", SERVER);
        builder.appendQueryParameter("db", DB);

        String query = builder.build().getEncodedQuery();

        // Open connection for sending data
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        conn.connect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    try {
        int response_code = conn.getResponseCode();

        // Check if successful connection made
        if (response_code == HttpURLConnection.HTTP_OK) {

            // Read data sent from server
            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            result = reader.readLine();
            return(result);

        }else{
            return("unsuccessful");
        }

When I go to my url (hidden in the variable getURL) using a browser, I see string of json on my screen, just as it should. However, when I output the contents of the reader (above code only takes the first line, but by adapting the code I can, of course, output more) it shows the html-code for a website displaying a 404 - Page does not exist message.

Anyone has any idea what goes wrong? Yes, I did check for typo's.

  • 写回答

1条回答 默认 最新

  • dongshou2024 2016-09-16 07:19
    关注

    Okay, I have no clue what happened, as I didn't change anything. But all of the sudden it started working?!?

    Must have been something server-side I guess...

    Thanks for the input and sharing your thoughts!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建