-2

When I try to retrieve a JSON object from an API, it causes an error. I'm using JSONObjectRequest to get the result.

This is my activity method:

private void execute(final Context context) {
    String url = UrlManager.getUrl(context, R.string.signup_url);
    User user = User.getInstance();
    try {
        url += "&name=" + mFirstName + "&last=" + mLastName + "&mob=" + mMobile +
            "&email=" + mEmail +
            "&pass=" + UrlManager.prepareUrlPart(mPassword);
    } catch (Exception e) {
        e.printStackTrace();
    }


    final ProgressDialog pDialog;
    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);
    pDialog.show();

    Response.Listener < JSONObject > listener = new Response.Listener < JSONObject > () {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject userObj = new JSONObject();
                userObj = response; //response.getJSONObject("UserInfo");
                userInfo = new UserInfo();
                userInfo.LoadItem(context, (JSONObject) userObj);
                String state = response.getString("State");
                if (state == "1") {
                    // Load user data from json and save them into local database
                    //  User user = User.getInstance();
                    // user.loadFromJSon(response);
                    /// user.saveUser();

                    onResult(true);
                } else {
                    onResult(false);
                }


            } catch (Exception e) {
                Log.d("Error", e.getMessage());
                e.printStackTrace();
            }
            pDialog.dismiss();
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
            pDialog.dismiss();
        }
    };

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, listener, errorListener);
    MyApplication.getInstance().addToRequestQueue(request);
}

I've assured that my JSON is non-empty. Here's a sample JSON result:

{"UserId":60,"UserName":"09111","Password":"123","Email":"m@m.com","Mobile":"09111111111","NationalId":"0000000000","FirstName":"mm","LastName":"mm","FatherName":"","Birthday":"1990/10/11","State":"1"}

This is the error in question:

org.json.JSONException: End of input at character 0 of 
Zoe
  • 27,060
  • 21
  • 118
  • 148
milad
  • 19
  • 7

1 Answers1

1
    private void execute(final Context context)
                {
                    String url = UrlManager.getUrl(context, R.string.signup_url);
                    User user = User.getInstance();
                    try {
                        url +=  "&name=" + mFirstName + "&last=" + mLastName + "&mob=" + mMobile  +
                                "&email=" + mEmail+
                                "&pass=" + UrlManager.prepareUrlPart(mPassword);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    final ProgressDialog pDialog;
                    pDialog = new ProgressDialog(context);
                    pDialog.setMessage("Loading...");
                    pDialog.setCancelable(false);
                    pDialog.show();

                    Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
                    {
                        @Override
                        public void onResponse(JSONObject response)
                        {
                            try
                            {
                             JSONObject userObj = response;
 userInfo = new UserInfo();
     userInfo.LoadItem(context,
     userObj); 
    String state = userObj.getString("State");
     if (state .equals("1")) { 
    // Load user data from json and save them into local database 
    // User user = User.getInstance(); 
    // user.loadFromJSon(response);
     /// user.saveUser();
     onResult(true); 
    } else { 
    onResult(false); 
    }
      }
     catch (Exception e)
                            {
                                Log.d("Error", e.getMessage());
                                e.printStackTrace();
                            }
                            pDialog.dismiss();
                        }
                    };

                    Response.ErrorListener errorListener = new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error)
                        {
                            Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
                            pDialog.dismiss();
                        }
                    };

                    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,null, listener, errorListener);
                    MyApplication.getInstance().addToRequestQueue(request);
                }
  • I tested two more mistakes were there : u were using wrong variable and using == to compare strings in java try { JSONObject userObj = new JSONObject(response); String state = userObj.getString("State"); if (state .equals("1")) { // Load user data from json and save them into local database // User user = User.getInstance(); // user.loadFromJSon(response); /// user.saveUser(); Log.e("val",state); } } catch (JSONException e) { e.printStackTrace(); } – Er. Pratik Chatterjee May 19 '18 at 12:18
  • when using JSONObject userObj = new JSONObject(response); i get error cannot resolve constructor 'JSONObject (org.json.JSONOBJECT)' @Er.-Pratik-Chatterjee – milad May 19 '18 at 12:23
  • oh sorry i didnt saw response is a JsonObject and coded as a string u can assign: JSONObject userObj = response; or directly use response. will update the code i posted. – Er. Pratik Chatterjee May 19 '18 at 12:29
  • updated the code.copy n paste it – Er. Pratik Chatterjee May 19 '18 at 12:30
  • I changed this and Revealed the same error : org.json.JSONException: End of input at character 0 @Er.-Pratik-Chatterjee – milad May 19 '18 at 12:32
  • will check in my studio n update. – Er. Pratik Chatterjee May 19 '18 at 12:33
  • can you post the stack trace once? – Er. Pratik Chatterjee May 19 '18 at 12:36
  • class com.android.volley.ParseError – milad May 19 '18 at 13:05
  • it seems like there is an issue in the response/format of json from server as volley is showing parsing error. can u check if there is any new line or whitespace in beginning or end? or can u send me the api request details so that i can check – Er. Pratik Chatterjee May 19 '18 at 13:10
  • there is a bug in api.. if data is different then response is given else empty string is returned instead of json object. http://www.carnawash.ir/fa/Android/Signup?k=MILSA_ANDROID_CARNAWASH&a=andr&name=aaa&last=ddd&mob=09100000000&email=m@m.com&pass=123456 i mean there may be the code in api if the registration is successful then response is returned and i user is already there then nothing is returned. that time volley id getting empty response and throwing error at 0 length of string – Er. Pratik Chatterjee May 19 '18 at 13:23
  • Okay, I'll test this – milad May 19 '18 at 13:29
  • thank you . problem solved. i love youuuu – milad May 19 '18 at 13:39
  • Welcome milad. Can u kindly accept the answer if u dnt need any futher help on this? – Er. Pratik Chatterjee May 19 '18 at 14:15