douyinghuo8874 2016-07-10 09:49
浏览 824

java.net.ProtocolException:意外的状态行:<!DOCTYPE HTML PUBLIC“ - // IETF // DTD HTML 2.0 // EN”>

I am new to Java and Android programming, I'm trying to get the result of a PHP file on my server but I get the following exception:

java.net.ProtocolException: Unexpected status line: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:905)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:789)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at it.codesnippet.dots.LoginActivity$1$override.run(LoginActivity.java:49)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at it.codesnippet.dots.LoginActivity$1$override.access$dispatch(LoginActivity.java)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at it.codesnippet.dots.LoginActivity$1.run(LoginActivity.java:0)
07-10 11:18:54.112 24683-25081/it.codesnippet.dots W/System.err:     at java.lang.Thread.run(Thread.java:818)

This is the code (in LoginActivity.java) where the exception comes from:

Thread loginCheckThread = new Thread(new Runnable() {
    public void run() {
        String result = "", dataObj = "";
        TextView username = (TextView) findViewById(R.id.login_mail);
        TextView password = (TextView) findViewById(R.id.login_password);

        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("loginMail", username.getText());
            jsonObj.put("loginPassword", password.getText());
            dataObj = jsonObj.toString(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            URL url = new URL("http://myserver.it/testandroid.php?data=" + dataObj);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            try {
/* line #49 */  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                int i = in.read();
                while (i != -1) {
                    bo.write(i);
                    i = in.read();
                }
                result = bo.toString();
            } catch (IOException e) {
                result = "Exception: Class::IOStream Related";
                e.printStackTrace();

            } finally {
                urlConnection.disconnect();
            }
        } catch (IOException e) {
            result = "Exception: Class::URL Related";
        }

        System.out.println(result);
    }
});
loginCheckThread.start();

The line which causes the exception is line #49:

InputStream in = new BufferedInputStream(urlConnection.getInputStream());

The PHP file is just a test file for now and is supposed to return <input_username> - <input_password> retrieved from the data variable in the query string:

$jsonObj = json_decode($_GET["data"], TRUE);

echo $jsonObj["loginMail"] . " - " . $jsonObj["loginPassword"];

Is about two days that this problem blocked me, what can I do to solve this?

  • 写回答

2条回答 默认 最新

  • drau67562 2016-07-10 10:12
    关注

    I'm not yet allowed to leave a comment, therefore I need to write an answer. There is a difference between HTTP and HTML. HTTP is used to download data (such as HTML). Your PHP script generates the downloadable content, but the HTTP client doesn't seem to think that it is the content already. As an example I'd like to show you how a HTTP response usually looks like:

    HTTP/1.1 200 OK
    Date: Sun, 10 Jul 2016 10:02:42 GMT
    Content-Type: text/html
    Content-Length: 6920
    Last-Modified: Sun, 10 Jul 2016 04:00:22 GMT
    Connection: keep-alive
    Accept-Ranges: bytes
    
    Here goes the content.
    

    As you can see the HTTP information appears until a double newline or CRLF (carriage return, line feed) appears. I've seen some server implementing that with LF only and that might be the problem, since your Java client isn't able to see when the content starts. Lets make a test: add an echo " "; right at the start of your PHP file to print the CRLFCRLF manually. If it then works, it is your server software's output which is wrong. Not your script and not your client. However, the RFC 2616 also says that it is recommended to ignore the CR and only watch for an LF:

    The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR. RFC 2616

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序