如何优化通过 php / android 从 mysql 获取数据?
How to optimize getting data from mysql via php / android?
简介
我有一个应用程序,如果 google 游戏市场上有新版本,我想强制用户更新它。这适用于以下代码,但不是很稳定。
我正在通过 AsyncTask 执行此操作并从 php 文件获取数据。
但是给我的感觉是一定有更好的办法或者有办法让它稳定很多。
问题
我如何为该过程设置超时,例如因为用户的互联网连接非常低?
代码
get_newest_apk.php
<?PHP
/* GET APK VERSION FROM ANDROID DEVICE
Example: [PATH]/get_newest_apk.php?apkversion=6
*/
if($_GET["apkversion"]){
$apkversion= $_GET["apkversion"];
//MYSQL LOGIN PARAMETERS
$host = '*****';
$user = '*****';
$pass = '*****';
$db = '*****';
$mysqli = new mysqli($host, $user, $pass, $db);
$result = $mysqli->query("SELECT MAX(VERSION) FROM TBL_APK");
$row = $result->fetch_row();
$count = $row[0];
if($count > $apkversion){
//Newer APK is avaiable
echo "1";
}else{
//There is no never APK avaiable
echo "2";
}
}else{
//Error by GETTING apkversion from Android device
echo "3";
}
?>
异步任务Class
class checkForNewAPK extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
//Do not show the user a progress because he don't want to see it in every onResume
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
int intResult = Integer.parseInt(s);
//strResult as intResult; 1 = Newer APK avaiable, 2 = No newer APK avaiable, 3 = Error at $_GET["apkversion"] line 6
if(intResult == 1){
//Force to Update App
Log.v("APKResult: ", "1");
}else if(intResult == 2){
//No update needed
Log.v("APKResult: ", "2");
}else if(intResult == 3){
//Error in PHP-File
Log.v("APKResult: ", "3");
}else{
//Unknown error
Log.v("APKResult: ", "Unkown Error");
}
}
//in this method we are fetching the json string
@Override
protected String doInBackground(Void... voids) {
try {
int intApkVersion = getApplication().getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
URL url = new URL(strUrlGetNewestAPK+"?apkversion="+intApkVersion);
String strResultFromEcho;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder strResult = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((strResultFromEcho = bufferedReader.readLine()) != null) {
strResult.append(strResultFromEcho + "\n");
}
return strResult.toString().trim();
} catch (Exception e) {
return null;
}
}
}
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
您需要使用setConnectTimeout
and setReadTimeout
setConnectTimeout
- 设置指定的超时值(以毫秒为单位),以在打开通信 link 到此 URLConnection 引用的资源时使用。如果在建立连接之前超时到期,则会引发 java.net.SocketTimeoutException。零超时被解释为无限超时。
setReadTimeout
- 将读取超时设置为指定的超时,以毫秒为单位。非零值指定在与资源建立连接时从输入流读取时的超时。如果超时在有数据可供读取之前到期,则会引发
java.net.SocketTimeoutException
。零超时被解释为无限超时。
示例代码
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
在您的代码中进行以下更改
class checkForNewAPK extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
//Do not show the user a progress because he don't want to see it in every onResume
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
int intResult = Integer.parseInt(s);
//strResult as intResult; 1 = Newer APK avaiable, 2 = No newer APK avaiable, 3 = Error at $_GET["apkversion"] line 6
if(intResult == 1){
//Force to Update App
Log.v("APKResult: ", "1");
}else if(intResult == 2){
//No update needed
Log.v("APKResult: ", "2");
}else if(intResult == 3){
//Error in PHP-File
Log.v("APKResult: ", "3");
}else{
//Unknown error
Log.v("APKResult: ", "Unkown Error");
}
}
//in this method we are fetching the json string
@Override
protected String doInBackground(Void... voids) {
try {
int intApkVersion = getApplication().getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
URL url = new URL(strUrlGetNewestAPK+"?apkversion="+intApkVersion);
String strResultFromEcho;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time outt in milliseconds
con.setReadTimeout(3000); // set read time outt in milliseconds
StringBuilder strResult = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((strResultFromEcho = bufferedReader.readLine()) != null) {
strResult.append(strResultFromEcho + "\n");
}
return strResult.toString().trim();
} catch (SocketTimeoutException e) {
// this block code execute when time out exception is occur
// you need to perform your logic here when time out exception occur
return "Request timeout occur.\nTap on \'TRY AGAIN\' to retry";
}
}
}
注意:您还可以使用库进行 API 调用
You can use Volley
library
- Volley 是一个 HTTP 库,它使 Android 应用程序的网络更容易,最重要的是,速度更快。 Volley 在 GitHub.
可用
来自文档
Volley offers the following benefits:
自动调度网络请求。
多个并发网络连接。
具有标准 HTTP 缓存一致性的透明磁盘和内存响应缓存。
支持请求优先级。
取消请求API。您可以取消单个请求,也可以设置要取消的请求块或范围。
易于定制,例如重试和退避。
强大的排序功能可以轻松地使用从网络异步获取的数据正确填充 UI。
调试和跟踪工具。
You can use Retrofit
library
- 用于 Android 和 Java 的类型安全 HTTP 客户端,由 Square, Inc. 提供。
Retrofit Github link
Retrofit vs Volley
请阅读此post
简介
我有一个应用程序,如果 google 游戏市场上有新版本,我想强制用户更新它。这适用于以下代码,但不是很稳定。 我正在通过 AsyncTask 执行此操作并从 php 文件获取数据。 但是给我的感觉是一定有更好的办法或者有办法让它稳定很多。
问题
我如何为该过程设置超时,例如因为用户的互联网连接非常低?
代码
get_newest_apk.php
<?PHP
/* GET APK VERSION FROM ANDROID DEVICE
Example: [PATH]/get_newest_apk.php?apkversion=6
*/
if($_GET["apkversion"]){
$apkversion= $_GET["apkversion"];
//MYSQL LOGIN PARAMETERS
$host = '*****';
$user = '*****';
$pass = '*****';
$db = '*****';
$mysqli = new mysqli($host, $user, $pass, $db);
$result = $mysqli->query("SELECT MAX(VERSION) FROM TBL_APK");
$row = $result->fetch_row();
$count = $row[0];
if($count > $apkversion){
//Newer APK is avaiable
echo "1";
}else{
//There is no never APK avaiable
echo "2";
}
}else{
//Error by GETTING apkversion from Android device
echo "3";
}
?>
异步任务Class
class checkForNewAPK extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
//Do not show the user a progress because he don't want to see it in every onResume
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
int intResult = Integer.parseInt(s);
//strResult as intResult; 1 = Newer APK avaiable, 2 = No newer APK avaiable, 3 = Error at $_GET["apkversion"] line 6
if(intResult == 1){
//Force to Update App
Log.v("APKResult: ", "1");
}else if(intResult == 2){
//No update needed
Log.v("APKResult: ", "2");
}else if(intResult == 3){
//Error in PHP-File
Log.v("APKResult: ", "3");
}else{
//Unknown error
Log.v("APKResult: ", "Unkown Error");
}
}
//in this method we are fetching the json string
@Override
protected String doInBackground(Void... voids) {
try {
int intApkVersion = getApplication().getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
URL url = new URL(strUrlGetNewestAPK+"?apkversion="+intApkVersion);
String strResultFromEcho;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder strResult = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((strResultFromEcho = bufferedReader.readLine()) != null) {
strResult.append(strResultFromEcho + "\n");
}
return strResult.toString().trim();
} catch (Exception e) {
return null;
}
}
}
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
您需要使用setConnectTimeout
and setReadTimeout
setConnectTimeout
- 设置指定的超时值(以毫秒为单位),以在打开通信 link 到此 URLConnection 引用的资源时使用。如果在建立连接之前超时到期,则会引发 java.net.SocketTimeoutException。零超时被解释为无限超时。
setReadTimeout
- 将读取超时设置为指定的超时,以毫秒为单位。非零值指定在与资源建立连接时从输入流读取时的超时。如果超时在有数据可供读取之前到期,则会引发
java.net.SocketTimeoutException
。零超时被解释为无限超时。
示例代码
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
在您的代码中进行以下更改
class checkForNewAPK extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
//Do not show the user a progress because he don't want to see it in every onResume
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
int intResult = Integer.parseInt(s);
//strResult as intResult; 1 = Newer APK avaiable, 2 = No newer APK avaiable, 3 = Error at $_GET["apkversion"] line 6
if(intResult == 1){
//Force to Update App
Log.v("APKResult: ", "1");
}else if(intResult == 2){
//No update needed
Log.v("APKResult: ", "2");
}else if(intResult == 3){
//Error in PHP-File
Log.v("APKResult: ", "3");
}else{
//Unknown error
Log.v("APKResult: ", "Unkown Error");
}
}
//in this method we are fetching the json string
@Override
protected String doInBackground(Void... voids) {
try {
int intApkVersion = getApplication().getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
URL url = new URL(strUrlGetNewestAPK+"?apkversion="+intApkVersion);
String strResultFromEcho;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time outt in milliseconds
con.setReadTimeout(3000); // set read time outt in milliseconds
StringBuilder strResult = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((strResultFromEcho = bufferedReader.readLine()) != null) {
strResult.append(strResultFromEcho + "\n");
}
return strResult.toString().trim();
} catch (SocketTimeoutException e) {
// this block code execute when time out exception is occur
// you need to perform your logic here when time out exception occur
return "Request timeout occur.\nTap on \'TRY AGAIN\' to retry";
}
}
}
注意:您还可以使用库进行 API 调用
You can use
Volley
library
- Volley 是一个 HTTP 库,它使 Android 应用程序的网络更容易,最重要的是,速度更快。 Volley 在 GitHub. 可用
来自文档
Volley offers the following benefits:
自动调度网络请求。
多个并发网络连接。
具有标准 HTTP 缓存一致性的透明磁盘和内存响应缓存。 支持请求优先级。
取消请求API。您可以取消单个请求,也可以设置要取消的请求块或范围。
易于定制,例如重试和退避。
强大的排序功能可以轻松地使用从网络异步获取的数据正确填充 UI。
调试和跟踪工具。
You can use
Retrofit
library
- 用于 Android 和 Java 的类型安全 HTTP 客户端,由 Square, Inc. 提供。 Retrofit Github link
Retrofit vs Volley
请阅读此post