-3

I know that this issue is well known, but I can't solve it so far.
In the end, I have a project and the data is stored perfectly in the DB (localhost). I'm using www.000webhost.com. The problem isn't in the PHP files or something with the connection of my app. Source https://www.youtube.com/watch?v=QxffHgiJ64M&t=70s&list=PLe60o7ed8E-TztoF2K3y4VdDgT6APZ0ka&index=1.
The issue happens with

JSONObject jsonResponse = new JSONObject(response);

In this SO post org.json.JSONException: End of input at character somebody wrote that the solution is:

Change

JSONObject jsonObject = new JSONObject(result);

to

result=getJSONUrl(url);  //<< get json string from server
JSONObject jsonObject = new JSONObject(result);

but in my app the method getJSONUrl doesn't exist, so that option doesn't work for me.
Any advice, my friends?

RegisterActivity.java

package com.tonikamitv.loginregister;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText etAge = (EditText) findViewById(R.id.etAge);
        final EditText etName = (EditText) findViewById(R.id.etName);
        final EditText etUsername = (EditText) findViewById(R.id.etUsername);
        final EditText etPassword = (EditText) findViewById(R.id.etPassword);
        final Button bRegister = (Button) findViewById(R.id.bRegister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = etName.getText().toString();
                final String username = etUsername.getText().toString();
                final int age = Integer.parseInt(etAge.getText().toString());
                final String password = etPassword.getText().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            response=getJSONUrl(response);  
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success) {
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

RegisterRequest.java

package com.tonikamitv.loginregister;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "https://yourURL/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("age", age + "");
        params.put("username", username);
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

the error I get

5-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err: org.json.JSONException: End of input at character 0 of 
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.tonikamitv.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:43)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.tonikamitv.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:39)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at android.os.Looper.loop(Looper.java:135)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5910)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
    05-02 22:17:35.391 1041-1041/com.tonikamitv.loginregister W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
    05-02 22:24:07.721 1041-1041/com.tonikamitv.loginregister D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    05-02 22:25:31.951 1041-1041/com.tonikamitv.loginregister W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
Community
  • 1
  • 1
Alien Java
  • 75
  • 2
  • 12

1 Answers1

2

in this stackover issue org.json.JSONException: End of input at character somebody wrote that the solution is this

(Some code)

Well, that question wasn't using Volley. So, response=getJSONUrl(response); isn't correct for your situation.

so the problem it isnt the php files or something with the connection of my app

How did you test that? Are you making a POST request to your PHP files outside of your Android application?

The same question you linked to also says...

Please check PHP File URL in Browser if get the data means you have problem in android side if browser not shows the data it means problem in PHP file

If you are checking your PHP scripts, do you get any JSON data returned?

Your error says you are getting an empty page returned.

End of input at character 0

An empty page isn't a JSON object, and so the problem is, in fact, originating in the PHP.

You can also test in Android like this

    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                Log.d("response", "Size: "+response.length());

If you see Size: 0 in the logs, you get an empty string.

Volley also provides a JsonObjectRequest class, by the way, making new JSONObject(response) not necessary.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • my friend the data is store perfectly in the DB. the data is saved! i can see the information in my table (localhost). so i think the connection is fine. not? – Alien Java May 03 '17 at 04:45
  • The database might be fine, but the POST request needs to echo some JSON back to the page – OneCricketeer May 03 '17 at 04:46
  • @AlienJava try to print your output in logcat , you must be getting empty string. – Prashant May 03 '17 at 04:46
  • 1
    @cricket_007 thanks a lot! i removed echo in the php files some minutes ago because in other stackover issue i read that without "echo" works fine! but the solution was write again "echo" before json_encode($response); thanks a lot!!!! – Alien Java May 03 '17 at 04:56
  • Welcome. Just a reminder: You don't need 000webhost to run a local web server & database – OneCricketeer May 03 '17 at 04:58