0

i am trying to send some user data (its registration android page) to my server host but there is an error while trying to get json object from server side, so how can i fix this problem?

update :: i am using byethost services and i understand that there is something with cookies. so how can i solve this problem using volley library?

i have a method called onResponse() in my RegisterActivity class and its where the problem begins. in this method i wrote this line which always throws exception about not converting string to json object which i think there is nothing to do with my java codes, the reason it cant convert string to json i guess is that its not a valid json object. but i dont know why it is not valid.

here is my register.php file https://www.androidtutorialpoint.com/wp-content/uploads/2016/09/register.txt https://www.androidtutorialpoint.com/wp-content/uploads/2016/09/update_user_info.txt https://www.androidtutorialpoint.com/wp-content/uploads/2016/09/android_login_connect.txt https://www.androidtutorialpoint.com/wp-content/uploads/2016/09/android_login_config.txt

by the way i i just followed a tutorial i do not know so much about php.

here is the method in my RegisterActivity class where the exception triggers.

            @Override
            public void onResponse(String response) {

            Toast.makeText(RegisterActivity.this, response, 
            Toast.LENGTH_LONG).show();

            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) {
                    String user = 
                    jObj.getJSONObject("user").getString("name");
                    Toast.makeText(getApplicationContext(), "Hi " + user + 
                     ", You are successfully Added!", 
                    Toast.LENGTH_SHORT).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            RegisterActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {

                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

i expect to have a valid json object to pass the user data to my server but what i already have is some invalid json which throws exceptions.

1 Answers1

0

well if anyone have followed the tutorials for register and login page and it does not work here are some reasons. 1) there is a problem with your php file which mostly its this one. 2) if there are problem with converting string to json object, print the string and see if its a valid json. 3)if everything is fine with these stuff, probably you should check the database and see if everything can fit with the php and user data and etc.

my problem was neither of those, as it turns out my server host have something to do with cookies (byethost) and you should add some headers for solving the problem here is a good link How to set custom header in Volley Request but i just solved my problem by using another server host (000host) and it works just fine. by the way if you are trying to use wamp and it doesn't work too, just use free 000host. it's been three days i was stuck on this thing. good luck