Bigdesaster
Bigdesaster

Reputation: 1

Android Studio HttpURLConnection every second request is empty on host

i'm trying to handle some json-requests with an php-server. Im using HttpURLConnection and AsyncTask on android and file_get_contents('php://input') on php.

My problem is that only every second request contains data. Sending data to php via HTML-Page and XHR every request is sucessfull. So i think problem is on android site. This is my code:

public class MySQLConnector extends AsyncTask<String, String, String>{
    private static final String LOG_TAG = MySQLConnector.class.getSimpleName();
    private static final String COOKIES_HEADER = "Set-Cookie";
    private Globals globals = Globals.getInstance();
    private Context context;
    private HttpURLConnection urlConnection;
    static java.net.CookieManager msCookieManager = new java.net.CookieManager();

    /****************************************
     *   INIT CONNECTOR AND EXECUTION
     **************************************/
    public MySQLConnector(Context c){
        context = c;
        Log.v(LOG_TAG, "MysqlConnector: init with context:" + c);
    }

    /**
     * Execute Task ( Send Request )
     * @param params Array param[0]=URL, param[1]=PostData (JSON-String)
     * @return
     */
    protected String doInBackground(String... params) {
        String phpScriptUrl = params[0].toString();
        String postData = params[1].toString();
        int contentLen = 0;

        try {
            publishProgress(context.getString(R.string.db_connecting));
            URL url = new URL(phpScriptUrl);

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("Accept","application/json");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setConnectTimeout(globals.getHttpConnectionTimeout());
            urlConnection.connect();  // Tried it with and without connect()

            Log.d(LOG_TAG, "send request to: " + phpScriptUrl + " / " + postData );

            publishProgress(context.getString(R.string.db_sending_request));
            OutputStream wr = new BufferedOutputStream(urlConnection.getOutputStream());
            wr.write(postData.getBytes(StandardCharsets.UTF_8));
            wr.flush();
            wr.close();

            int code = urlConnection.getResponseCode();
            if (code !=  HttpURLConnection.HTTP_OK) {
                throw new IOException("Invalid response from server: " + code);
            }

            publishProgress(context.getString(R.string.db_receiving_response));
            contentLen = urlConnection.getContentLength();
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream()));
            StringBuffer responseBuffer = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null) {
                responseBuffer.append(line);
            }
            String response = responseBuffer.toString();
            publishProgress(context.getString(R.string.db_sending_request), "100");
            Log.d(LOG_TAG, "received response: " + response);

            return response;
        } catch (Exception e) {
           Log.e(LOG_TAG, "HTTP-Exception: " + e.getMessage());
           return e.getMessage().toString();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

    protected void onPreExecute(){
    }

    protected void onPostExecute(String result){
        super.onPostExecute(result);
        if (result != null) {
            mCompleteListener.onComplete(result);
        }
    }

I expect that every request will containt data. In Debugger the 'postData' are filled correctly.

Upvotes: 0

Views: 25

Answers (0)

Related Questions