-6ren"> -我是 Java 和 Android 编程新手,我试图获取服务器上 PHP 文件的结果,但出现以下异常: java.net.ProtocolException: Unexpected status li-6ren">
gpt4 book ai didi

java.net.ProtocolException : Unexpected status line: <! DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

转载 作者:行者123 更新时间:2023-12-02 03:29:34 24 4
gpt4 key购买 nike

我是 Java 和 Android 编程新手,我试图获取服务器上 PHP 文件的结果,但出现以下异常:

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)

这是异常来自的代码(在 LoginActivity.java 中):

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();

导致异常的行是第#49行:

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

PHP 文件目前只是一个测试文件,应该返回 <input_username> - <input_password>检索自data查询字符串中的变量:

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

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

这个问题困扰了我大约两天,我该怎么解决这个问题?

最佳答案

重要

对于在 Volley HTTP 实现中遇到此错误的任何人:

java.net.ProtocolException: Unexpected status line: <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>

请检查并使用OkHttp依赖来解决它。 Here the documentation to check.

就我而言(我不知道为什么),我的 Android 测试手机 (MOTO E V.4.4.4 Kitkat) 给我返回了 java.net.ProtocolException: Unexpected status line异常(exception)。我尝试重置手机并删除应用程序,但没有任何效果。

另一方面,我的 Nexus 5X(带有 Android Oreo)运行良好(相同的应用程序、相同的网络、相同的后端)。

请(真的)尝试 OkHttp 依赖项,它会起作用。

Psd:一些 stackoverflow 线程提到了 Volley Cache 或类似的东西,尝试了我自己的一切,但是 OkHttp 解决了我的麻烦。

关于java.net.ProtocolException : Unexpected status line: &lt;! DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com