Search code examples
phpandroidandroid-asynctaskhttpurlconnection

Android inputStream and outputStream


I was trying to use HTTP-Post to get some MySQL database information. Using HttpUrlConnection and OutputStreamWriter, I get:

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)

The code is:

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){
           ...
        }
    }

Some say it's the problem of PHP file, but I ran a PHP validation, it's nothing to do with. What's the problem?


Solution

  • You need to move the call to "getResponseCode()" to below all the outputstream/printwriter writing. HTTP response codes are only available after the request is fully sent; it's not possible to write additional data to the http server after reading the http response code.