我试图使用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验证,这与无关。 怎么了?
您需要将对"getResponseCode(("的调用移动到所有输出流/打印机写入的下方。HTTP 响应代码仅在请求完全发送后可用;读取 HTTP 响应代码后,无法将其他数据写入 HTTP 服务器。