0

This is my code for requesting the JSONObject. While working with String request it worked perfectly but i need JSONObject.

    Map<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    params.put("password", password);
    params.put("request", "login");

    JSONObject jsonObj = new JSONObject(params);

    String url="http://example.com/android/login.php";
    JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,jsonObj,
            new Response.Listener<JSONObject>()
            {


                @Override
                public void onResponse(JSONObject response) {
                    // response
                   // JSONObject json = JSONObject.toJson(response);
                   // Toast.makeText(Login.this, "Your ID is ."+response, Toast.LENGTH_LONG).show();

                    //Log.d("Response", response.toString());

                    Intent r = new Intent(Login.this,Cus_home.class);
                    startActivity(r);

                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.toString());
                }
            }
    );

    mRequestQueue.add(postRequest);

On checking the API output the output is : [{"id":"77","type":"1"}]

But after running the android code, I get this

Error.Response: com.android.volley.ParseError: org.json.JSONException: End of input at character 1 of  

Any help help is really appreciated

Omkar Frozen
  • 572
  • 6
  • 16
  • Have you seen http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley ? It suggests using this syntax as follows: `final JSONObject jsonBody = new JSONObject("{\"type\":\"example\"}");` – Simon Phoenix Aug 31 '16 at 12:18
  • I am having issues receiving JsonObject. Not sure if that can help me – Omkar Frozen Aug 31 '16 at 12:24
  • @SimonPhoenix JsonObject is from my input fields. Please suggest something more – Omkar Frozen Aug 31 '16 at 12:32

1 Answers1

0

You are returning invalid JSON Object. You are returning a JSON String instead of object.

JSON objects are written inside curly braces. e.g. {"firstName":"John", "lastName":"Doe"}`

your onResonse method would look like this

public void onResponse(JSONObject response) {
                    Log.d("response", response.optString("id") + " " +
                            response.optString("type"));

}
saty
  • 66
  • 7
  • Yes I understood that. Now I get The out put as {"id":"77","type":"1"} , But even then I still get same error on Response.ErrorListener() – Omkar Frozen Aug 31 '16 at 12:51
  • check my updated answer i have tested it. everything is working fine. – saty Aug 31 '16 at 13:16