Question

[Solved] org.json.JSONException: End of input at character 0 of, android

I am implementing an android app and trying to make a register and login screens. my app is connected to a server, here is the part my code that makes error:

public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error)
                {
                    // User successfully stored in MySQL
                    // Now store the user in sqlite
                    String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String email = user.getString("email");
                    String created_at = user
                            .getString("created_at");

                    // Inserting row in users table
                    db.addUser(name, email, uid, created_at);

                    Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

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

                    // Error occurred in registration. Get the error
                    // message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                    Log.d("Debug", errorMsg);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.toString());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

and here is my error:
org.json.JSONException: End of input at character 0 of.
actually I have seen similar questions in this site but i was not be able to solve my problem

UPDATE:
this is my logcat content, and there is nothing in front of response:

 1576-1576/com.example.neshat.androidhive2 D/RegisterActivity? Register Response:
12-21 09:43:08.820    1576-1576/com.example.neshat.androidhive2 W/System.err? org.json.JSONException: End of input at character 0 of
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at org.json.JSONTokener.nextValue(JSONTokener.java:97)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at org.json.JSONObject.<init>(JSONObject.java:156)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at org.json.JSONObject.<init>(JSONObject.java:173)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.example.neshat.androidhive2.RegisterActivity$3.onResponse(RegisterActivity.java:124)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.example.neshat.androidhive2.RegisterActivity$3.onResponse(RegisterActivity.java:116)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at android.os.Handler.handleCallback(Handler.java:739)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at android.os.Handler.dispatchMessage(Handler.java:95)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at android.os.Looper.loop(Looper.java:135)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at android.app.ActivityThread.main(ActivityThread.java:5221)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at java.lang.reflect.Method.invoke(Native Method)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at java.lang.reflect.Method.invoke(Method.java:372)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-21 09:43:08.824    1576-1576/com.example.neshat.androidhive2 W/System.err? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Enquirer: Neshat

||

Solution #1:

As you have seen in your logcat 1576-1576/com.example.neshat.androidhive2 D/RegisterActivity? Register Response: which is the result of Log.d(TAG, "Register Response: " + response.toString());, response is an empty string, so your app will get org.json.JSONException: End of input at character 0 of at the line JSONObject jObj = new JSONObject(response);.

You should check response is not null and not an empty string first.

Respondent: Neshat

Solution #2:

If you are using php to get the response, you should check on your php file. I had the same problem as you, i forgot to write the response on my php file. Below is part of my code that i forgot

$response = new emp();
$response->success = 1;
$response->message = "Register success, please login.";
die(json_encode($response)); 
Respondent: BNK

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy