Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onRun() is never called #14

Closed
globus243 opened this issue Mar 10, 2015 · 3 comments
Closed

onRun() is never called #14

globus243 opened this issue Mar 10, 2015 · 3 comments

Comments

@globus243
Copy link

onRun() is never called for me. I require Network and have ACCESS_NETWORK_STATE set in my Manifest.

// as you stated in another ticket, I tested this
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
boolean connected =  netInfo != null && netInfo.isConnectedOrConnecting();
// connected returns true

This is my Job:

public class HTTPJobExecution extends Job {

    public static final int PRIORITY = 1;
    private boolean error = false;
    private String errorMsg = null;
    private SimpleDateFormat s = new SimpleDateFormat("hh:mm:ss.SSS");
    private String format = s.format(new Date());
    public String URL;
    public Context context;

    public HTTPJobExecution(String uri, Context GivenContext){
        super(new Params(PRIORITY).requireNetwork().persist());

        URL = uri;
        Log.v("In the Job: ", URL);
        context = GivenContext;

    }

    @Override
    public void onAdded() {

    }

    @Override
    public void onRun() throws Throwable {

        error = false;
        Log.v("", "I'ts: " + format + " URL: " + URL); // for testing

        int timeoutSocket       = 3000;
        int timeoutConnection   = 3000;

        try{

            Log.v("", "I'ts: " + format + " URL: " + URL); // for testing

            HttpGet httpGet = new HttpGet(URL);
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

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

        } catch (ClientProtocolException e) {

            Log.v("Server not Reachable: ", URL);
            errorMsg = "ClientProtocolException URL: " + URL;
            error = true;

        }catch (ConnectTimeoutException a){

            Log.e("Server not Reachable: ", URL);
            errorMsg = "ClientProtocolException URL: " + URL;
            error = true;

        } catch (HttpHostConnectException e) {

            Log.e("Server not Reachable: ", URL);
            errorMsg = "ClientProtocolException URL: " + URL;
            error = true;
        }

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

    }

    @Override
    protected void onCancel() {

    }

    @Override
    protected boolean shouldReRunOnThrowable(Throwable throwable) {
        return false;
    }
}

this is how I called it
jobManager.addJobInBackground(new HTTPJobExecution(s, context)); //s is a simple String
an how i initialized it:
jobManager = new JobManager(context);

even if I delete requireNetwork() and everything in onRun() (except a Log for check) it is not called

@mendhak
Copy link

mendhak commented Mar 11, 2015

I just had the same problem and I think it's because of what you pass in the constructor. In your case, you are passing a Context, in my case I was passing a Location object. I believe neither of those are Serializable. If you remove the context from your call it'll probably work (removing Location worked for me).

That means you'll need to pass in the values you need directly or create a serializable object that holds those values. From your code sample it looks like you don't need Context though.

tl;dr - pass only serializable things in.

@mecid
Copy link

mecid commented Mar 12, 2015

Use transient keyword for fields which can't be serialize like Context

@yigit
Copy link
Owner

yigit commented Apr 10, 2015

yep, you are passing context to a persistent app. Instead, make your Application class singleton and grab it from there. See #18 , I'll add a getContext method in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants