0

I want to access my Android apps through Web-Service. but getting error in my android app ,

i am using volley & POST method for login.. `public class Main extends AppCompatActivity implements View.OnClickListener { public static final String LOGIN_URL = "http://10.54.103.8:4067/evivaservices/Webservices/login";

public static final String KEY_USERNAME="username";
public static final String KEY_PASSWORD="password";

private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonLogin;

private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);

    editTextUsername = (EditText) findViewById(R.id.username1);
    editTextPassword = (EditText) findViewById(R.id.password1);

    buttonLogin = (Button) findViewById(R.id.login_button);

    buttonLogin.setOnClickListener(this);
}
private void userLogin() {
    username = editTextUsername.getText().toString().trim();
    password = editTextPassword.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        Next();
                    }
                    catch(JSONException e)
                    {
                    e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(),error.toString(), Toast.LENGTH_LONG ).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<String,String>();
            map.put(KEY_USERNAME,username);
            map.put(KEY_PASSWORD,password);
            return map;
        }
    };

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

private void Next(){
    Intent intent = new Intent(this, HomeScreen.class);
    intent.putExtra(KEY_USERNAME, username);
    startActivity(intent);
}
@Override
public void onClick(View v)
{
userLogin();
}

} `

JSON DATA


{
  "id": 31,
  "username": "[email protected]",
  "user_image": "http:\/\/103.54.103.8:4067\/evivaservices\/img\/profile_31.png",
  "err-code": 0
}

`

5
  • You should use JsonRequest instead of StringRequest. Commented Aug 1, 2016 at 10:28
  • Your code should not compile in first instance, as you are missing a ;. To solve your issue, remove the line JSONArray jsonArray=jsonObject.getJSONArray("err-code") Commented Aug 1, 2016 at 10:31
  • @chandeshwar Thakur : Did that solve your problem? Commented Aug 1, 2016 at 12:28
  • yes bro...i solved .this.by JsonObjectRequest method..thanx for suggest me...but i dont know why my reputation decreased Commented Aug 1, 2016 at 12:33
  • @chandeshwar Thakur: That is because someone downvoted your question :( Commented Aug 2, 2016 at 5:02

3 Answers 3

1

Please change your StringRequest to JsonRequest as below:

 JsonRequest jsonRequest = new JsonRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<>() {
        @Override
        public void onResponse(Object response) {
            try {
                Next();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put(KEY_USERNAME, username);
            map.put(KEY_PASSWORD, password);
            return map;
        }

        @Override
        protected Response parseNetworkResponse(NetworkResponse response) {
            return null;
        }

        @Override
        public int compareTo(Object another) {
            return 0;
        }
    };

Thank You.

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

Comments

0

Hello dear you made a mistake while you parsing the response

 JSONArray jsonArray=jsonObject.getJSONArray("err-code")

remove above the line and then parse again.

Comments

0

In you JSON DATA, I not find array, so you shouldn't use JsonArray, you can delete this line in your code :JSONArray jsonArray=jsonObject.getJSONArray("err-code").

3 Comments

check it code properly...getting error at JSONObject jsonObject = new JSONObject(response);
Please explain why this fixes the problem
@AesSedai101 JSONArray is used for JSON data has this snipped like "result" : [ {"value1": 1} , {"value2":2} , {"value3":3} ]. you konw JSON data has array. In this case, you must use JSONArray class.

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.