Toast in AsyncTask in non Activity class

微笑、不失礼 提交于 2019-12-12 10:49:49

问题


I have a non Activity class which contains an AsyncTask which calls an URL. If the Connection Timeouts for some reasons, I want to let the user know this by posting a Toast. But I just can't get any Context.

How can achive this?

RPIcall.class

import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.IOException;

class RPicall extends AsyncTask<String, String, Void> {

    @Override
    protected Void doInBackground(String... uri) {

        int timeoutSocket       = 3000;
        int timeoutConnection   = 3000;

        try{

            Log.v("call URL: ", uri[0]);

            HttpGet httpGet = new HttpGet(uri[0]);
            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            // The default value is zero, that means the timeout is not used. 

            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.

            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpResponse response = httpClient.execute(httpGet);

        } catch (ClientProtocolException e) {
            //Here Connection TimeOut excepion
            //Toast.makeText(, "Your connection timedout", Toast.LENGTH_SHORT).show();
            Log.v("Server not Reachable: ", uri[0]);
            publishProgress("TimeOut");

        } catch (HttpHostConnectException e) {

            Log.e("Server not Reachable: ", uri[0]);

        }

        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        String test = "test";
        Log.v("finished: ", test);

    }
}

回答1:


Step #1: Add a constructor to RPicall that takes a Context, storing it in a data member.

Step #2: Use that Context in onPostExecute() to display your Toast.

Step #3: Pass in a suitable Context when you create an instance of RPicall.




回答2:


define a constructor that takes a Context object as parameter, and keep it as class member. For instance:

class RPicall extends AsyncTask<String, String, Void> {

       private final Context mContext;
       public RPicall(Context c) {
          mContext = c;
       }

 }


来源:https://stackoverflow.com/questions/28659165/toast-in-asynctask-in-non-activity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!