1

This is program but getting JSON exception.

private void uploadImage(){

    String uploadUrl = "http://...........";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, uploadUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String Response = jsonObject.getString("response");
                Toast.makeText(MainActivity.this,Response,Toast.LENGTH_SHORT).show();
                mImage.setImageResource(0);
                mImage.setVisibility(View.GONE);
                mName.setText("");
                mName.setVisibility(View.GONE);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    })

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("photo_one",imageString(bitmap));
            params.put("mobile",mName.getText().toString().trim());

            return params;
        }
    };
    MySingleton.getInstance(MainActivity.this).addToRequestQue(stringRequest);

}

This is the error I am getting.

org.json.JSONException: End of input at character 0 of at org.json.JSONTokener.syntaxError(JSONTokener.java:449) at org.json.JSONTokener.nextValue(JSONTokener.java:97) at org.json.JSONObject.(JSONObject.java:156) at org.json.JSONObject.(JSONObject.java:173) at com.example.athis.practiceproject4.MainActivity$1.onResponse(MainActivity.java:93)

Which is JSONObject jsonObject = new JSONObject(response); line. What is wrong?

3
  • check your response . Commented Aug 23, 2017 at 6:58
  • is your response valid ? and is it json object/array ? Commented Aug 23, 2017 at 7:00
  • what is the value of response string Commented Aug 23, 2017 at 7:01

3 Answers 3

2

org.json.JSONException: End of input at character 0 of at

You are getting an empty response.

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

Comments

1

check you response as error org.json.JSONException: End of input at character 0 of at indicates that your response string is null or 0, kindly check for your response string first.

Comments

0

Try using below code;

  HashMap<String, String> params = new HashMap<>();
  params.put("photo_one",imageString(bitmap));
  params.put("mobile",mName.getText().toString().trim());

    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
    progressDialog.setMessage("Fetching Data....");
    progressDialog.show();

    JsonObjectRequest req = new JsonObjectRequest(uploadUrl, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    progressDialog.dismiss();

                    String success = (String) response.opt("success");
                    if (success.equals("1") )
                    {

                        // Do your Task here....

                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Invalid Username & Password...",Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            progressDialog.dismiss();

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

3 Comments

What should i write in // Do your Task here... ? I just want to upload one image and text, which i think params.put is already done.
Simply put Toast there for successfully image uploaded
Getting D/Volley: [46298] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] Whats wrong?

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.