user2592029
user2592029

Reputation: 71

End of input at character 0 error during json parsing

I am new to the android, so what is happening that when i am trying to parse the json which am getting from the mysql database using the below code:

public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);




            users = jsonObject.getJSONArray(JSON_ARRAY);

            //ids = new String[users.length()];
            names = new String[users.length()];
            message = new String[users.length()];

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
               // ids[i] = jo.getString(KEY_ID);
                names[i] = jo.getString(KEY_NAME);
                message[i] = jo.getString(KEY_EMAIL);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

i am getting the following error as mentioned below:

Error:

04-24 20:01:44.969 13858-13858/? W/System.err: org.json.JSONException: End of input at character 0 of 
04-24 20:01:44.969 13858-13858/? W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
04-24 20:01:44.969 13858-13858/? W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
04-24 20:01:44.969 13858-13858/? W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:154)
04-24 20:01:44.979 13858-13858/? W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:171)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.weavearound.schools.weavearound.ParseJSON.parseJSON(ParseJSON.java:31)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.weavearound.schools.weavearound.list_view.showJSON(list_view.java:75)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.weavearound.schools.weavearound.list_view.access$000(list_view.java:22)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.weavearound.schools.weavearound.list_view$1.onResponse(list_view.java:50)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.weavearound.schools.weavearound.list_view$1.onResponse(list_view.java:44)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
04-24 20:01:44.979 13858-13858/? W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
04-24 20:01:44.979 13858-13858/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:605)
04-24 20:01:44.979 13858-13858/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 20:01:44.979 13858-13858/? W/System.err:     at android.os.Looper.loop(Looper.java:137)
04-24 20:01:44.979 13858-13858/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4517)
04-24 20:01:44.979 13858-13858/? W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 20:01:44.989 13858-13858/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 20:01:44.989 13858-13858/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
04-24 20:01:44.989 13858-13858/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
04-24 20:01:44.989 13858-13858/? W/System.err:     at dalvik.system.NativeStart.main(Native Method)

list_view where i fetch the data

public class list_view extends AppCompatActivity implements View.OnClickListener {

    public static final String JSON_URL = "http://www.schools.weavearound.com/chatdb_api/showback.php";

    private Button buttonGet;

    private ListView listView;

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

        buttonGet = (Button) findViewById(R.id.buttonGet);
        buttonGet.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);
    }

    private void sendRequest(){


        StringRequest stringRequest = new StringRequest(JSON_URL,
                new Response.Listener<String>() {



                    @Override
                    public void onResponse(String response) {
                        showJSON(response);
                    }
                },


                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(list_view.this,error.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }




    private void showJSON(String json){

        final String TAG = events.class.getSimpleName();
        Log.d(TAG, "showJSON: \n" + json);

        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        CustomList cl = new CustomList(this,ParseJSON.names,ParseJSON.message);
        listView.setAdapter(cl);
    }

    @Override
    public void onClick(View v) {
        sendRequest();
    }
}

Upvotes: 1

Views: 1123

Answers (2)

Dan Oprean
Dan Oprean

Reputation: 56

I got the same error and fixed it with giving permision in the android manifest for internet. Always remember to add this code to your manifest when parsing json from the net

<uses-permission android:name="android.permission.INTERNET" />

<application
    ...>

Upvotes: 1

Atul Dwivedi
Atul Dwivedi

Reputation: 34

In my opinion you are actually defining thing in different way , If you are doing parsing then please take every jsonObject or JsonArray as local. see i am providing you code:

try {
           JsonObject jsonObject = new JSONObject(json);

           JsonArray users = jsonObject.getJSONArray(JSON_ARRAY);

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
                String name = jo.getString(KEY_NAME);
                String message = jo.getString(KEY_EMAIL);


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

Please apply in your code and let me know it will work.

Upvotes: 1

Related Questions