Android Studio + PHP json_decode() returning null

Viewed 22

In the Android studio, the Json form was made into a string type and sent to php. It was received by PHP at $_POST ['attrArr']. it changes to array well on Chrome. However, if I check gettype($data) on Android, it appears as NULL. console in Android

//console in Android
response - gettype($data): NULL

// php run on chrome
gettype($data): array

Android used AsyncTask.

route_result.GetData task = new route_result.GetData();
task.execute("http://10.0.2.2/route_re.php", resultObject.toString());
class GetData extends AsyncTask<String, Void, String> {

        ProgressDialog progressDialog;
        String errorString = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = ProgressDialog.show(route_result.this,
                    "잠시만 기다려주세요.", null, true, true);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            progressDialog.dismiss();
            //mTextViewResult.setText(result);
            Log.d(TAG, "response - " + result);

            if (result == null){

                mTextViewResult.setText(errorString);
            }
            else {
                mJsonString = result;
                //Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                showResult();
            }
        }

        @Override
        protected String doInBackground(String... params) {

            String attrArr = (String)params[1];

            String serverURL = (String)params[0];//"http://10.0.2.2/charger.php";
            String postParameters = "attrArr=" + attrArr ;

            try {

                URL url = new URL(serverURL);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.connect();

                OutputStream outputStream = httpURLConnection.getOutputStream();
                outputStream.write(postParameters.getBytes("UTF-8"));
                outputStream.flush();
                outputStream.close();

                int responseStatusCode = httpURLConnection.getResponseCode();
                Log.d(TAG, "POST response code - " + responseStatusCode);

                InputStream inputStream;
                if(responseStatusCode == HttpURLConnection.HTTP_OK) {
                    inputStream = httpURLConnection.getInputStream();
                }
                else{
                    inputStream = httpURLConnection.getErrorStream();
                }

                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder sb = new StringBuilder();
                String line;

                while((line = bufferedReader.readLine()) != null){
                    sb.append(line);
                }

                bufferedReader.close();

                return sb.toString();

            } catch (Exception e) {

                Log.d(TAG, "InsertData: Error ", e);

                return new String("Error: " + e.getMessage());
            }
        }
    }


route.php

$attrArr = isset($_POST['attrArr']) ? $_POST['attrArr'] : '';
$data = json_decode($attrArr, true);

$data = json_decode(stripslashes($attrArr), true); I've also used it, but it doesn't seem meaningless.

What should I do?

1 Answers

Are you sure "console in Android" works asynchronously? It might be NULL until Post request was made.

Also

       @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(route_result.this,
                "잠시만 기다려주세요.", null, true, true);
    }

super.onPreExecute(); is probably not async function.

Related