PDA

View Full Version : مشکل در ساخت یک کلاس async task



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();

#root#
چهارشنبه 08 مرداد 1393, 20:27 عصر
الان شما یه کلاس ناشناس ساختی فقط ، اجراش نکردی :
new isconnect().execute("yourInput_if_has");

ho3ein.3ven
چهارشنبه 08 مرداد 1393, 20:31 عصر
دقیقا همین الان متوجه این اشتباه شدم . ممنون بابت پاسختون . ولی با این کار بازم درست نشد .

لاگ کت :

07-30 12:31:46.225: D/AndroidRuntime(852): Shutting down VM
07-30 12:31:46.225: W/dalvikvm(852): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-30 12:31:46.305: D/dalvikvm(852): GC_FOR_ALLOC freed 149K, 8% free 2870K/3100K, paused 32ms, total 46ms
07-30 12:31:46.326: E/AndroidRuntime(852): FATAL EXCEPTION: main
07-30 12:31:46.326: E/AndroidRuntime(852): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-30 12:31:46.326: E/AndroidRuntime(852): at android.view.ViewRootImpl.setView(ViewRootImpl.jav a:563)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.view.WindowManagerGlobal.addView(WindowMan agerGlobal.java:269)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.view.WindowManagerImpl.addView(WindowManag erImpl.java:69)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.app.Dialog.show(Dialog.java:281)
07-30 12:31:46.326: E/AndroidRuntime(852): at com.example.downloader.MainActivity$isconnect.onPr eExecute(MainActivity.java:240)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.os.AsyncTask.executeOnExecutor(AsyncTask.j ava:586)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.os.AsyncTask.execute(AsyncTask.java:534)
07-30 12:31:46.326: E/AndroidRuntime(852): at com.example.downloader.MainActivity$1.onClick(Main Activity.java:72)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.view.View.performClick(View.java:4240)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.view.View$PerformClick.run(View.java:17721 )
07-30 12:31:46.326: E/AndroidRuntime(852): at android.os.Handler.handleCallback(Handler.java:730 )
07-30 12:31:46.326: E/AndroidRuntime(852): at android.os.Handler.dispatchMessage(Handler.java:92 )
07-30 12:31:46.326: E/AndroidRuntime(852): at android.os.Looper.loop(Looper.java:137)
07-30 12:31:46.326: E/AndroidRuntime(852): at android.app.ActivityThread.main(ActivityThread.jav a:5103)
07-30 12:31:46.326: E/AndroidRuntime(852): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:31:46.326: E/AndroidRuntime(852): at java.lang.reflect.Method.invoke(Method.java:525)
07-30 12:31:46.326: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:737)
07-30 12:31:46.326: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553)
07-30 12:31:46.326: E/AndroidRuntime(852): at dalvik.system.NativeStart.main(Native Method)
07-30 12:33:58.685: I/Choreographer(894): Skipped 268 frames! The application may be doing too much work on its main thread.
07-30 12:33:58.755: D/gralloc_goldfish(894): Emulator without GPU emulation detected.
07-30 12:33:58.946: I/Choreographer(894): Skipped 108 frames! The application may be doing too much work on its main thread.

#root#
چهارشنبه 08 مرداد 1393, 21:43 عصر
ولی با این کار بازم درست نشد .
قرار هم نبود درست بشه با اون تیکه :چشمک:

الان مشکل از این خطه :
pd = new ProgressDialog(getApplicationContext());

و به طور دقیقتر از getApplicationContext() ، بجاش یه context از جنس اکتیویتی بدین بهش

ho3ein.3ven
چهارشنبه 08 مرداد 1393, 23:08 عصر
آقا خیلی ممنون . واقعا لطف کردین .