ho3ein.3ven
چهارشنبه 08 مرداد 1393, 17:45 عصر
سلام . من قصد دارم یک کلاس async task بنویسم که بتونه از یک آدرس پینگ بگیره . کلاس رو به صورت زیر نوشتم ولی جواب نمیده . از اساتید گرامی خواهشمندم کمک کنن .
private ProgressDialog pd;
class isconnect extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
//showDialog(progress_bar_type);
pd = new ProgressDialog(getApplicationContext());
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
ConnectivityManager cm = (ConnectivityManager)getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
try {
URL url = new URL("http://www.google.com/");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(100); // Timeout
urlc.connect();
if (urlc.getResponseCode() == 200) {
return "true";
} else {
return "false";
}
} catch (IOException e) {
Log.i("warning", "Error checking internet connection", e);
return "false";
}
}
return "false";
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), file_url, 1).show();
if (file_url.equals("true"))
{
Connect=true;
}
else
{
Connect=false;
}
}
}
به صورت زیر فراخوانیش می کنم :
new isconnect();
private ProgressDialog pd;
class isconnect extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
//showDialog(progress_bar_type);
pd = new ProgressDialog(getApplicationContext());
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
ConnectivityManager cm = (ConnectivityManager)getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
try {
URL url = new URL("http://www.google.com/");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(100); // Timeout
urlc.connect();
if (urlc.getResponseCode() == 200) {
return "true";
} else {
return "false";
}
} catch (IOException e) {
Log.i("warning", "Error checking internet connection", e);
return "false";
}
}
return "false";
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), file_url, 1).show();
if (file_url.equals("true"))
{
Connect=true;
}
else
{
Connect=false;
}
}
}
به صورت زیر فراخوانیش می کنم :
new isconnect();