首页 > 解决方案 > 有没有办法通过 Android 应用程序中的 StringRequest URL 访问 AWS RDS 数据库?

问题描述

我对服务器系统非常陌生,并且一直在努力让登录系统在我正在开发的 android 应用程序上运行。

我将 RDS 数据库与 AWS EC2 服务器连接,但不知道如何使用应用程序访问 RDS 数据库。

这是我用于服务器的类:

公共类 ValidateRequest 扩展 StringRequest {

final static private String URL = "https://localhost/UserValidate.php";
private Map<String, String> parameters;

//    send parameter values to database by posting method
public ValidateRequest(String userID, Response.Listener<String> listener) {
    super(Method.POST, URL, listener, null);
    parameters = new HashMap<>();
    parameters.put("userID",userID);
}

@Override
protected Map<String, String> getParams(){
    return parameters;
}

}

程序执行时,不显示任何错误消息。我认为我的 URL 变量设置错误,但不知道如何修复它。谁能建议在这里做什么?

我的 php 文件位于 /var/www/html 远程站点下。任何帮助将不胜感激。

标签: androidmysqlamazon-web-servicesserveramazon-rds

解决方案


我意识到这已经晚了一个月,但我猜这对任何有同样问题的人都会有用。此答案假定您已为 rds 实例进行了必要的安全组设置(例如使其可公开访问,尽管我只建议出于开发目的这样做)。

这个答案也使用了 volley,尽管对请求队列使用了单例类。

解决方案 -

1. PHP 常量文件。(声明你的数据库常量)

define ('DB_HOST',  'aws rds access point goes here');

define ('DB_USER', 'rds user name goes here ' );

define ('DB_PASSWORD', 'rds password goes here ');

2. PHP 连接文件。(启动连接)

require_once "constants.php";

$con = new mysqli(DB_HOST,DB_USER,DB_PASSWORD);
if($con)
{

 $sql = "SQL Query";

 $result = mysqli_query($con,$sql);

 //Whatever you echo here will be treated as the response at the android end
 //Can be JSON,string etc.

}

3.Java文件。(在android中发起String请求)

这是一个示例,说明如果您尝试将用户登录到您的应用程序中会是什么样子。

 private void login(final String emailText, final String passText) {
        final StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_SHORT).show();
                System.out.println("Error is " + error.toString());
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map <String,String> params  = new HashMap<String,String>();

                params.put(Constants.KEY_EMAIL,emailText);
                params.put(Constants.KEY_PASSWORD,passText);

                return params;
            }
        };
        MySingleton.getInstance(this).addToRequestQueue(request);
    }

4. Java 单例类。(如果您提出很多请求,建议使用)

public class MySingleton {
    private static MySingleton instance;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private static Context ctx;

    private MySingleton(Context context) {
        ctx = context;
        requestQueue = getRequestQueue();

        imageLoader = new ImageLoader(requestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (instance == null) {
            instance = new MySingleton(context);
        }
        return instance;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
        }
        return requestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}

推荐阅读