【发布时间】:2016-07-10 09:49:41
【问题描述】:
我是 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 文件现在只是一个测试文件,应该返回从查询字符串中的data 变量检索到的<input_username> - <input_password>:
$jsonObj = json_decode($_GET["data"], TRUE);
echo $jsonObj["loginMail"] . " - " . $jsonObj["loginPassword"];
这个问题阻塞了我大概两天,我该怎么做才能解决这个问题?
【问题讨论】:
-
如果不深入研究这个问题,我可以建议使用 OkHttp 进行网络调用吗?它对我很有帮助,它可能会为你解决这个问题。
-
听起来很像HTTP响应无效;如果是这样,问题出在服务器端,而不是引用的代码。
-
@T.J.Crowder - 服务器文件工作正常,即使只是回显“A”从 Java 调用它也不起作用
-
我只是说,这个错误是一个协议异常,当它试图读取 HTTP 响应的状态行时,它得到了有效负载的第一行。这表明发送端有问题(或
HttpURLConnection中的错误,但selectisn't broken,所以...)。 -
谢谢@Vucko,我刚刚google了一下,找到了XD
标签: java php android httpurlconnection protocolexception