1

I'm using a PHP web service to make a login page. But in JSON response I've a string outside main JSON(check this link). I tried to parse it using this link as reference but I'm getting this JSON error. Can anyone help me in parsing this JSON in volley request?

public class MainActivity extends AppCompatActivity {

    private EditText username, password;
    private Button login;

    private static final String LOGIN = "http://demo.example.net/login.php";

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

        username = (EditText)findViewById(R.id.ET1);
        password = (EditText)findViewById(R.id.ET2);
        login = (Button)findViewById(R.id.btn);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String pass = password.getText().toString();

                if(!name.isEmpty() && !pass.isEmpty()){
                    attemptlogin();
                }
            }
        });
    }

    private void attemptlogin() {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();

                        try {
                            JSONObject jObj = new JSONObject(response);

                            JSONObject phone = jObj.getJSONObject("testtest123");

                            String status = phone.getString("success");

                            // Now check status value
                            if (status.equals("0")) {
                                Toast.makeText(getApplicationContext(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
                            } else if (status.equals("1")) {

                                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();



                            //    startActivity(new Intent(getApplicationContext(), SecondActivity.class));
                             //   finish();



                            } else {
                                String errorMsg = jObj.getString("error_msg");
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username.getText().toString());
                params.put("password", password.getText().toString());
                return params;
            }

        };

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

1 Answer 1

1

That is because you are trying to convert the whole string as Json Object and it cannot be done.

Inside your onResponse try this

        int index=response.indexOf("{");
       String jsonString= st.substring(index);
       JSONObject jObj = new JSONObject(jsonString);
       String status = jObj.getString("success");

String can be converted to JsonObject only if it starts with "{".

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

2 Comments

Thanks for the reply and explanation. What's "st" here in String jsonString= st.substring(index);?
@SomnathPal what is the "st" here ?

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.