Android 输入流和输出流
Posted
技术标签:
【中文标题】Android 输入流和输出流【英文标题】:Android inputStream and outputStream 【发布时间】:2019-01-25 19:43:07 【问题描述】:我试图使用 HTTP-Post 来获取一些 mysql 数据库信息。 使用 HttpUrlConnection 和 OutputStreamWriter,我得到:
08-19 21:47:38.883 28462-28668/com.squareb0x.playground W/System.err: java.net.ProtocolException: cannot write request body after response has been read
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:255)
at com.squareb0x.playground.LoadscreenActivity$getDB.doInBackground(LoadscreenActivity.java:231)
at com.squareb0x.playground.LoadscreenActivity$getDB.doInBackground(LoadscreenActivity.java:212)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
08-19 21:47:38.883 28462-28462/com.squareb0x.playground W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.squareb0x.playground.LoadscreenActivity.Backload_ResourcesDownload(LoadscreenActivity.java:165)
at com.squareb0x.playground.LoadscreenActivity$2.onPostExecute(LoadscreenActivity.java:147)
at com.squareb0x.playground.LoadscreenActivity$2.onPostExecute(LoadscreenActivity.java:137)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
代码是:
protected String doInBackground(String... params)
String serverURL = params[0];
try
url = new URL(serverURL);
if (httpURLConnection == null)
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setDefaultUseCaches(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
httpURLConnection.connect();
int response_status_code = httpURLConnection.getResponseCode();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("client").append(encryptString(playground_specific_channel, AES_playground_key, "AES"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter((httpURLConnection.getOutputStream()), "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.write(stringBuffer.toString());
printWriter.flush();
...
else
...
catch (MalformedURLException e)
...
catch (Exception e)
...
有人说是php文件的问题,但是我跑了一个PHP验证,没有关系。 有什么问题?
【问题讨论】:
【参考方案1】:您需要将对“getResponseCode()”的调用移至所有输出流/打印机写入的下方。 HTTP 响应代码仅在请求完全发送后可用;读取http响应码后,无法向http服务器写入额外数据。
【讨论】:
以上是关于Android 输入流和输出流的主要内容,如果未能解决你的问题,请参考以下文章