如何通过Android中的Web服务对用户进行身份验证?

时间:2012-11-05 12:31:39

标签: android web-services webservice-client

我对webservices很新。目前我正在开发一个需要通过webservices对用户进行身份验证的Android应用程序(意味着用户名和密码存储在远程数据库中)。谁能告诉我,我怎么能做到这一点?

2 个答案:

答案 0 :(得分:0)

你可以使用REST webservice,请查看以下链接

Android Connecting to remote server Mysql

答案 1 :(得分:0)

我不确定您使用的是哪些网络服务。如果你的Ksoap 2网络服务。

CheckNetwork C0de

 public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
       context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         //Toast.makeText(context, "No Internet Connection", 1000);
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
 }

在你的活动onCreate()

  Name =mEditTextUsername.getText().toString();
  Pass= mEditTextPassword.getText().toString();
  new TheTask().execute(Name,Pass);

class TheTask extends AsyncTask<String, Integer, Void>{
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        pd=new ProgressDialog(Login.this);
        pd.setTitle("Authenticating");
        pd.show();

    }

    @Override
    protected Void doInBackground(String... params) {   
        authenticate(params[0],params[1]);
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        pd.dismiss();
       }
    public void authenticate(String name, String pass)
    {
    if(CheckNetwork.isInternetAvailable(Login.this))
    {

        SoapObject request = new SoapObject("NameSpace", "method name");
                PropertyInfo loginreq = new PropertyInfo();
        loginreq.name="LoginReq";
        loginreq.type=String.class;
                    loginreq.setValue(your request value);
                    request.addProperty(loginreq);  
            SoapSerializationEnvelope envelop = new      SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelop.setOutputSoapObject(request);
            System.out.println("Request is"+request); 

        HttpTransportSE androidHttpTransport = new   HttpTransportSE ("your wsdl link");
        androidHttpTransport.debug=true;                                                                  androidHttpTransport.call("YOUR LOGINREQUST", envelop);
        SoapObject response=(SoapObject) envelop.bodyIn;
        System.out.println("Response is......"+response.toString());//get your response
    }   

Webservice响应代码标记1表示成功,0表示失败。这还取决于webservice响应的设计。 上面的代码使用soap webservice。另请看一下这个链接。 how to connect android and mysql server?