How to optimize getting data from mysql via php / android?
up vote
6
down vote
favorite
Introduction
I have an app and want to force the user to update it, if a new version is at the google play market. This is working fine with the following code but is not very stable.
I am doing it via AsyncTask and getting data from the php file.
But it gives me a feeling that there must be a better way or a way that makes it much more stable.
Question
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
Code
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";
}
?>
AsyncTask 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;
}
}
}
php android mysql httpurlconnection stringbuilder
add a comment |
up vote
6
down vote
favorite
Introduction
I have an app and want to force the user to update it, if a new version is at the google play market. This is working fine with the following code but is not very stable.
I am doing it via AsyncTask and getting data from the php file.
But it gives me a feeling that there must be a better way or a way that makes it much more stable.
Question
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
Code
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";
}
?>
AsyncTask 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;
}
}
}
php android mysql httpurlconnection stringbuilder
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Introduction
I have an app and want to force the user to update it, if a new version is at the google play market. This is working fine with the following code but is not very stable.
I am doing it via AsyncTask and getting data from the php file.
But it gives me a feeling that there must be a better way or a way that makes it much more stable.
Question
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
Code
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";
}
?>
AsyncTask 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;
}
}
}
php android mysql httpurlconnection stringbuilder
Introduction
I have an app and want to force the user to update it, if a new version is at the google play market. This is working fine with the following code but is not very stable.
I am doing it via AsyncTask and getting data from the php file.
But it gives me a feeling that there must be a better way or a way that makes it much more stable.
Question
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
Code
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";
}
?>
AsyncTask 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;
}
}
}
php android mysql httpurlconnection stringbuilder
php android mysql httpurlconnection stringbuilder
edited Nov 13 at 12:09
asked Nov 13 at 9:45
MSeiz5
1501420
1501420
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10
add a comment |
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
You need to use setConnectTimeout
and setReadTimeout
setConnectTimeout
- Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
setReadTimeout
- Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a
java.net.SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.
SAMPLE CODE
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
Make below changes in your code
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";
}
}
}
Note : You can also use library for API Calling
You can use
Volley
library
- Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
FROM DOCS
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
You can use
Retrofit
library
- Type-safe HTTP client for Android and Java by Square, Inc.
Retrofit Github link
Retrofit vs Volley
Please read this post Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
You need to use setConnectTimeout
and setReadTimeout
setConnectTimeout
- Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
setReadTimeout
- Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a
java.net.SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.
SAMPLE CODE
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
Make below changes in your code
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";
}
}
}
Note : You can also use library for API Calling
You can use
Volley
library
- Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
FROM DOCS
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
You can use
Retrofit
library
- Type-safe HTTP client for Android and Java by Square, Inc.
Retrofit Github link
Retrofit vs Volley
Please read this post Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
add a comment |
up vote
6
down vote
accepted
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
You need to use setConnectTimeout
and setReadTimeout
setConnectTimeout
- Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
setReadTimeout
- Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a
java.net.SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.
SAMPLE CODE
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
Make below changes in your code
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";
}
}
}
Note : You can also use library for API Calling
You can use
Volley
library
- Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
FROM DOCS
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
You can use
Retrofit
library
- Type-safe HTTP client for Android and Java by Square, Inc.
Retrofit Github link
Retrofit vs Volley
Please read this post Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
You need to use setConnectTimeout
and setReadTimeout
setConnectTimeout
- Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
setReadTimeout
- Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a
java.net.SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.
SAMPLE CODE
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
Make below changes in your code
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";
}
}
}
Note : You can also use library for API Calling
You can use
Volley
library
- Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
FROM DOCS
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
You can use
Retrofit
library
- Type-safe HTTP client for Android and Java by Square, Inc.
Retrofit Github link
Retrofit vs Volley
Please read this post Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
How can I set a timeout to the procedure, for example because of a very low internetconnection from the user?
You need to use setConnectTimeout
and setReadTimeout
setConnectTimeout
- Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
setReadTimeout
- Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a
java.net.SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.
SAMPLE CODE
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000); //set connection time out in milliseconds
con.setReadTimeout(3000); // set read time out in milliseconds
Make below changes in your code
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";
}
}
}
Note : You can also use library for API Calling
You can use
Volley
library
- Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
FROM DOCS
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache coherence.
Support for request prioritization.Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
Debugging and tracing tools.
You can use
Retrofit
library
- Type-safe HTTP client for Android and Java by Square, Inc.
Retrofit Github link
Retrofit vs Volley
Please read this post Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
answered Nov 16 at 9:30
Nilesh Rathod
28.3k82853
28.3k82853
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
add a comment |
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
1
1
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
upvoted for the clear and precise answer.
– Ümañg ßürmån
Nov 20 at 6:19
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
My question apart from this post, i am using Magento 1.9 soap v2 api, the values i getting like objects how can i convert into xml for Andriod Application Development. My code: justpaste.it/43kpl Thanks. @Nilesh Rathod
– Gem
Nov 22 at 6:17
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278091%2fhow-to-optimize-getting-data-from-mysql-via-php-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. Question is currently under the risk of being close-voted as Too Broad
– Madhur Bhaiya
Nov 13 at 10:24
I did edit the question. Thanks!
– MSeiz5
Nov 13 at 12:10