1

我有一个名为 menu.db 的数据库文件,该文件位于服务器上,现在我想从该数据库中读取数据。我正在处理的应用程序上也有注册页面,当用户按下提交按钮时,所有用户信息都应存储在服务器上的该数据库中。如果有人解决了这个问题,请帮助我。

如果有人知道,请帮助我。

4

1 回答 1

0

我有以下代码。它验证用户密码。您应该在 AsyncTask 扩展类的 doBackground() 中调用此方法。

public boolean authenticate(String strUsername, String strPassword)
{
    boolean bReturn = false;

    InputStream pInputStream = null;
    ArrayList<NameValuePair> pNameValuePairs = new ArrayList<NameValuePair>();
    pNameValuePairs.add(new BasicNameValuePair("userid", strUsername));
    pNameValuePairs.add(new BasicNameValuePair("password", strPassword));

    try
    {
        HttpClient pHttpClient = new DefaultHttpClient();

        String strURL = p_strServerIP + "Login.php";
        HttpPost pHttpPost = new HttpPost(strURL);
        pHttpPost.setEntity(new UrlEncodedFormEntity(pNameValuePairs));

        HttpResponse pHttpResponse = pHttpClient.execute(pHttpPost);
        HttpEntity pHttpEntity = pHttpResponse.getEntity();
        pInputStream = pHttpEntity.getContent();

        BufferedReader pBufferedReader = new BufferedReader(new InputStreamReader(pInputStream,"iso-8859-1"),8);

        StringBuilder pStringBuilder = new StringBuilder();
        String strLine = pBufferedReader.readLine();
        pInputStream.close();
        if(strLine != null)
        {
            if((strLine).equals("permit"))
            {
                bReturn = true;
            }
        }
    }
    catch (Exception e) 
    {
        Log.e("log_tag", "Caught Exception @ authenticate(String strUsername, String strPassword):" + e.toString());
    }

    return bReturn;
}

您从 AsyncTask 扩展的类应该类似于

class ConnectionTask extends AsyncTask<String, Void, Boolean> 
{
    private SharedPreferences mSettings;
    private Context mContext;
    ConnectionTask(SharedPreferences settings, Context context)
    {
        mSettings = settings;
        mContext = context;
    }

    protected void onProgressUpdate(Integer... progress) 
    {
    }

    protected void onPostExecute(Boolean result) 
    {
        Toast.makeText(mContext, "Authentication over.", Toast.LENGTH_LONG).show();
    }

    @Override
    protected Boolean doInBackground(String... params) 
    {
        pVerifier.authenticate(params[0], params[1]);
        return true;
    }
}
于 2012-12-19T13:04:10.073 回答