[英]Handling Server side exceptions on Android
我有一个Android应用程序,该应用程序连接到php网络服务器以获取数据并将数据设置到数据库中。
从Web服务器连接到我的数据库时,如果无法建立连接,则Web服务器可能会崩溃。 我的问题是在php中捕获错误然后让android应用程序知道出了点问题的正确方法是什么。
PHP sql连接:
$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检索
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);
您可以尝试将其添加到您的代码中, 从凌空错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.