如何在Web服务中存储和检索用户名和密码

时间:2014-05-07 05:03:52

标签: android soap

我是android新手。如何通过Web服务传递,存储和检索用户名和密码。我用肥皂制作了我的网络服务。

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

1 个答案:

答案 0 :(得分:2)

如果您想使用网络服务登录该应用程序,则需要执行以下操作

  • 编写一种方法,将用户名和密码作为参数传递给您的网络服务
  • 然后在您的onclick方法中,使用EditText
  • 中的用户名和密码
  • 并通过提供您的用户名和密码来调用您实施的方法

您可以使用AsyncTask来执行此操作。

以下是一些您可以对此进行了解的代码

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;

    }

上面的方法是将任何参数传递给所需的Web服务,并将Web服务的结果作为JsonObject返回。你必须提供你的参数以及网络服务网址。

这是检查登录的方法

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;
}

您可以在onclick方法中调用以下内容。

String username = yourEditTex.getText.tostring; 
String password=yourEditTex.getText.tostring; 
JSONObject authenticate = AuthenticateUser(username,password); 

list view code one

adapter

View