-1

i am making a login application on android i have used asynctask and Json to exchange data over server but i am getting this error i am adding the code.kindly help guys

    class RegisterUser extends AsyncTask<Void, Void, String> {

        private ProgressBar progressBar;

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("username", username);
            params.put("email", email);
            params.put("password", password);
            params.put("gender", gender);

            //returing the response
            return requestHandler.sendPostRequest(URLs.URL_REGISTER, 
        params);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //displaying the progress bar while user registers on the server
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //hiding the progressbar after completion
            progressBar.setVisibility(View.GONE);
            Log.i("JSON Parser", s);
            try {
                //converting response to json object
                JSONObject obj = new JSONObject(s);
              //  Log.i("JSON Parser", obj);

                //if no error in response
                if (!obj.getBoolean("error")) {
                    Toast.makeText(getApplicationContext(), 
     obj.getString("message"), Toast.LENGTH_SHORT).show();

                    //getting the user from the response
                    JSONObject userJson = obj.getJSONObject("user");

                    //creating a new user object
                    User user = new User(
                            userJson.getInt("id"),
                            userJson.getString("username"),
                            userJson.getString("email"),
                            userJson.getString("gender")
                    );

                    //storing the user in shared preferences


     SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                    //starting the profile activity
                    finish();
                    startActivity(new Intent(getApplicationContext(), 
  ProfileActivity.class));
                } else {
                    Toast.makeText(getApplicationContext(), "Some error 
 occurred", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d("error is","this");
            }
        }
    }

    //executing the async task
    RegisterUser ru = new RegisterUser();
    ru.execute();
   }

and the funny thing is the log.i below

  @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //hiding the progressbar after completion
            progressBar.setVisibility(View.GONE);
            Log.i("JSON Parser", s); 

is giving me the source code of both my php files

my php files are below

<?php

require_once 'DbConnect.php';

//an array to display response
$response = array();
mysqli_set_charset($con, 'utf8');
//if it is an api call 
//that means a get parameter named api call is set in the URL 
//and with this parameter we are concluding that it is an api call

if (isset($_GET['apicall'])) {

    switch ($_GET['apicall']) {

        case 'signup':
            //checking the parameters required are available or not 



            if (isTheseParametersAvailable
                            (array('username', 'email', 'password', 'gender'))) {

                //getting the values 
                $username = $_POST['username'];
                $email = $_POST['email'];
                $password = md5($_POST['password']);
                $gender = $_POST['gender'];

                //checking if the user is already exist with this username 
                or email
                //as the email and username should be unique for every user 
                $stmt = $conn->prepare("SELECT id FROM users WHERE username 
        = ? OR email = ?");
                $stmt->bind_param("ss", $username, $email);
                $stmt->execute();
                $stmt->store_result();

                //if the user already exist in the database 
                if ($stmt->num_rows > 0) {
                    $response['error'] = true;
                    $response['message'] = 'User already registered';
                    $stmt->close();
                } else {

                    //if user is new creating an insert query 
                    $stmt = $conn->prepare("INSERT INTO users (username, 
        email, password, gender) VALUES (?, ?, ?, ?)");
                    $stmt->bind_param("ssss", $username, $email, $password, $gender);

                    //if the user is successfully added to the database 
                    if ($stmt->execute()) {

                        //fetching the user back 
                        $stmt = $conn->prepare("SELECT id, id, username, 
          email, gender FROM users WHERE username = ?");
                        $stmt->bind_param("s", $username);
                        $stmt->execute();
                        $stmt->bind_result($userid, $id, $username, $email, $gender);
                        $stmt->fetch();

                        $user = array(
                            'id' => $id,
                            'username' => $username,
                            'email' => $email,
                            'gender' => $gender
                        );

                        $stmt->close();

                        //adding the user data in response 
                        $response['error'] = false;
                        $response['message'] = 'User registered 
               successfully';
                        $response['user'] = $user;
                    }
                }
            } else {
                $response['error'] = true;
                $response['message'] = 'required parameters are not 
            available';
            }

            break;

        case 'login':
            //for login we need the username and password 
            if (isTheseParametersAvailable(array('username', 'password'))) {
                //getting values 
                $username = $_POST['username'];
                $password = md5($_POST['password']);

                //creating the query 
                $stmt = $conn->prepare("SELECT id, username, email, gender 
               FROM users WHERE username = ? AND password = ?");
                $stmt->bind_param("ss", $username, $password);

                $stmt->execute();

                $stmt->store_result();

                //if the user exist with given credentials 
                if ($stmt->num_rows > 0) {

                    $stmt->bind_result($id, $username, $email, $gender);
                    $stmt->fetch();

                    $user = array(
                        'id' => $id,
                        'username' => $username,
                        'email' => $email,
                        'gender' => $gender
                    );

                    $response['error'] = false;
                    $response['message'] = 'Login successfull';
                    $response['user'] = $user;
                } else {
                    //if the user not found 
                    $response['error'] = false;
                    $response['message'] = 'Invalid username or password';
                }
            }
            break;

        default:
            $response['error'] = true;
            $response['message'] = 'Invalid Operation Called';
    }
} else {
    //if it is not api call 
    //pushing appropriate values to response array 
    $response['error'] = true;
    $response['message'] = 'Invalid API Call';
}

//displaying the response in json structure 
echo json_encode($response);

//function validating all the paramters are available
//we will pass the required parameters to this function 
function isTheseParametersAvailable($params) {

    //traversing through all the parameters 
    foreach ($params as $param) {
        //if the paramter is not available
        if (!isset($_POST[$param])) {
            //return false 
            return false;
        }
    }
    //return true if every param is available 
    return true;
}

i am new to android and java and learning on my own. i found this code on some website and tried to execute it but its giving this error. help

16
  • plz post userJson content Commented Sep 13, 2017 at 10:45
  • parse your string to JSON object Commented Sep 13, 2017 at 10:45
  • Check your error logs and ensure you are parsing correctly. Commented Sep 13, 2017 at 10:45
  • I think that you have no PHP installed on your server(since you get <?, which is the beginning of PHP file) Commented Sep 13, 2017 at 10:46
  • this is what i am gettting in my android monitor Commented Sep 13, 2017 at 10:47

1 Answer 1

0

Looks like your PHP file is invalid. it starts from <?, and missing php in the head. So instead of <?, use <?php in your PHP file.

When reading file, Apache (or whatever PHP server you use) reads it, and does not recognize it as PHP file. Therefore it processes it as a text file by just sending it's content as response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.