choi choi - 1 month ago 14
Android Question

How do i Log in in a webservice using my android App?

I am trying to log in in the website through my app.
I am using volley library. I am not able to figure out how should i send the data and what exactly is the response that i will get?

the URL is http://saptrishiherbal.com/home/login

public class MainActivity extends AppCompatActivity {

private static String TAG = MainActivity.class.getSimpleName();
private Button login;

private ProgressDialog pDialog;

private EditText username;
private EditText password;

String url = "http://www.saptrishiherbal.com/home/login";
String Username;
String Password;

public static final String KEY_USERNAME = "txtusername";
public static final String KEY_PASSWORD = "txtPassword";

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

username = (EditText) findViewById(R.id.editText5);
password = (EditText) findViewById(R.id.editText6);

Username = username.getText().toString().trim();
Password = password.getText().toString().trim();

pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);

login = (Button) findViewById(R.id.login);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Loginnow(Username, Password);
}
});
}

public void Loginnow(final String username1, final String password1) {

final HashMap<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, username1);
params.put(KEY_PASSWORD, password1);

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

Intent intent = new Intent(getBaseContext(), Home.class);
startActivity(intent);
}
},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
})

/* {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, username1);
params.put(KEY_PASSWORD, password1);

return params;
}

};*/

AppController.getInstance().addToRequestQueue(stringRequest);
showpDialog();
}

private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
}

Answer

You can use POSTMAN to hit the API url and view the response.

Login is usually a post request with the credentials in body of the app. If the credentials are in params and not body, it could be either GET or POST request.

Once you figured out the HTTP method type and where params are to be placed, you can create Volley request for that method and override getParams() or getBody() methods as required.