Question

I am new to android. How to pass,store and retrieve username and password form web-service. i built my web-service in soap.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.confirm);

        ipaddress = (EditText)findViewById(R.id.txt_Mail_ID);
        userid    = (EditText)findViewById(R.id.txt_uname);
        password  =(EditText)findViewById(R.id.txt_psw);
        login = (Button)findViewById(R.id.btn_Confirm);
        login.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            //  if (UserName.trim().length() > 0 && Password.trim().length() > 0) {
            // (UserName.equals("userid")&& Password.equals("password")) {

                Intent intent = new Intent(Confirm.this, select_company.class);

                Urltag.UrlTag = "LoginAuthentication";
                Bundle b = new Bundle();
                String UserName = userid.getText().toString();   
                b.putString("UserName", UserName);

                String Password = password.getText().toString();
                b.putString("Password", Password);
                intent.putExtras(b);


                if((userid.getText().toString()).equals(password.getText().toString())){
                Toast.makeText(Confirm.this, "Login Successfully", Toast.LENGTH_LONG).show();

                startActivity(intent);

                finish();
            }else {
                Toast.makeText(Confirm.this, "Please check username and password !", Toast.LENGTH_LONG).show();
            }
            }enter code here
Was it helpful?

Solution

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 from EditText
  • 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); 

list view code one

adapter

View

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top