If you want to login to the application using a web service, you need to do following things
- write a method to pass the username and the passwords as a parameter to your web service
- then in your
onclick
method, take the username and the password fromEditText
- and just call your implemented method by giving your username and the password
you can use AsyncTask
to do this.
Below is some codes which you can have some idea about this
public JSONObject getJSONFromUrl(JSONObject parm,String url) throws JSONException {
InputStream is = null;
JSONObject jObj = null;
String json = "";
// Making HTTP request
try {
// defaultHttpClient
/*JSONObject parm = new JSONObject();
parm.put("agencyId", 27);
parm.put("caregiverPersonId", 47);*/
/* if(!(jObj.isNull("d"))){
jObj=null;
}
*/
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
HttpEntity body = new StringEntity(parm.toString(), "utf8");
httpPost.setEntity(body);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
/* String response = EntityUtils.toString(httpEntity);
Log.w("myApp", response);*/
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// JSONObject jObj2 = new JSONObject(json);
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
above method is passing any parameter to a desired web service and return the result of the web service as a JsonObject
. you have to give your parameters and also the web service url.
this is a method to check the login
public JSONObject AuthenticateUser(final String user,final String pass){
// boolean status = false;
/*String authenticate = null;
String returnValue=null;*/
// String authenticatePersonID=null;
JSONObject authenticate1=new JSONObject() ;
JSONObject json = new JSONObject();
try {
JsonParser jpar = new JsonParser();
JSONObject userParam = new JSONObject();
userParam.put("username", user);
userParam.put("password", pass);
json = jpar.getJSONFromUrl(userParam,URL);
authenticate1 = json.getJSONObject("d");
} catch(Exception e) {
e.printStackTrace();
}
return authenticate1;
}
you can just call the following in your onclick
method.
String username = yourEditTex.getText.tostring;
String password=yourEditTex.getText.tostring;
JSONObject authenticate = AuthenticateUser(username,password);