我发现将OutputStreamWriter包装在BufferedWriter中是引起问题的原因,因此摆脱了包装器的困扰,我的POST通过了.仍然想知道为什么BufferedWriter是原因.
我先向自己道歉,因为我才刚刚开始自学有关数据库,应用程序与服务器/数据库之间的通信以及php的所有知识.
I apologize in advance, as I am just beginning to teach myself all about databases, communication between an app and a server/database, and php.
其他类似问题未提供答案.
Other similar questions asked have not provided an answer.
使用HttpUrlConnection从我的android应用程序到本地托管服务器上的php脚本进行简单POST时,我很难分辨出我所缺少的内容.我需要从android应用程序中获取用户ID,并将其作为参数传递给php脚本,然后在称为用户的数据库表中执行该ID的查找.我也想使用不被弃用的方法和类.
I am having some trouble discerning what I'm missing when it comes to making a simple POST using HttpUrlConnection from my android application to a php script on a locally hosted server. I need to take a user ID from the android application, pass that to the php script as an argument, and perform a lookup of that ID in the database table called users. I also would like to use methods and classes that are not deprecated.
我已在android清单中包含Internet权限.我正在使用XAMPP,并且已经验证我的服务器正在运行,并且如果我通过Web浏览器访问该URL,则会得到我正在寻找的JSON响应.
I have included Internet permissions in the android manifest. I am using XAMPP, and I have verified that my servers are running, and if I access the url through a web browser I get the JSON response I am looking for.
我唯一的logcat消息是IOException,它在写入输出流后立即发生. 特定的例外是意外的流结束" .
My only logcat message is an IOException, which occurs just after writing to the output stream. The specific exception is "unexpected end of stream".
这是我的AsyncTask类中的代码:
Here is the code in my AsyncTask class:
protected String doInBackground(String... params) { String userID = params[0]; Pair<String, String> phpParameter = new Pair<>("userID", userID); String result = ""; try { URL url = new URL("url of locally-hosted php script"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // prepare request urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setReadTimeout(10000); urlConnection.setConnectTimeout(15000); urlConnection.setFixedLengthStreamingMode(userID.getBytes("UTF-8").length); // upload request OutputStream outputStream = urlConnection.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(outputStream, "UTF-8")); writer.write(phpParameter.first + "=" + phpParameter.second); writer.close(); outputStream.close(); // read response BufferedReader in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); result = response.toString(); // disconnect urlConnection.disconnect(); } catch (MalformedURLException e) { Log.e("Malformed URL Exception", "Malformed URL Exception"); } catch (IOException e) { Log.e("IOException", "IOException"); } return result; }这是服务器上的我的php脚本.对于准备好的语句,我也需要一些帮助,所以不要因为'id = 1'而惹恼我:
And here is my php script on the server. I also need a little help with prepared statements, so don't beat me up over the 'id = 1':
<?php mysql_connect("host","username","password"); mysql_select_db("Database"); print($_POST); $sql = mysql_query("select * from users where id = 1"); while($row = mysql_fetch_assoc($sql)) $output[] = $row; print(json_encode($output)); // this will print the output in json mysql_close(); ?>推荐答案
好像我已将其修复!
我改变了
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(outputStream, "UTF-8")); writer.write(phpParameter.first + "=" + phpParameter.second);到
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); writer.write(userID);由于某种原因,将输出流写入器包装在缓冲写入器中的行为破坏了它.我不知道为什么.
For some reason, the act of wrapping the output stream writer in a buffered writer was breaking it. I don't know why.
进行了上述更改后,我收到一条错误消息,说该流原本需要1个字节,但接收到8个字节,所以我只写了userID,这是我始终传递给setFixedLengthStreamingMode的内容.
Once I made the above change, I received an error saying that the stream was expecting 1 byte but received 8, so I wrote only userID, which is what I passed to setFixedLengthStreamingMode anyway.
更多推荐
如何使用HttpUrlConnection通过POST传递参数
发布评论