简体   繁体   中英

Handling Server side exceptions on Android

I have an android application that connects to a php webserver to fetch and set data into a database.

When connecting to my database from the webserver, if the connection cannot be established the webserver expectedly crashes. My question is what is the correct way to catch an error in php and then let the android application know that something has gone wrong.

PHP sql connect:

$this->dbConn = mysqli_connect ( $this->host, $this->dbUser, $this->dbPass );
if (! $this->dbConn) {
    trigger_error ( 'could not connect to server' );
}
mysqli_set_charset ( $this->dbConn, "utf8" );

Android retrieval

    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, ConnectionInfo.IMAGE_RETRIEVE,
            response -> {
                Log.d("VolleyResponse",response);
                // Error needs to be caught before it can be processed
                byte[] decodedString = Base64.decode(response, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                imageView.setImageBitmap(decodedByte);
            }, error -> {
        Log.d("VolleyResponse","Error fetching image: " + error);
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("img", "");
            return params;
        }
    };
    queue.add(stringRequest);

您可以尝试将其添加到您的代码中, 从凌空错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM