0

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

4

2 回答 2

0

您可以使用 REST 网络服务,请查看以下链接

Android连接远程服务器Mysql

于 2012-11-05T12:51:48.387 回答
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工作。也看看这个链接。如何连接android和mysql服务器?.

于 2012-11-05T13:03:32.190 回答