1

I have a service that runs in background. It is registered in manifest. When I pass a couple strings to it from my activity and start it, the intent is null. I have checked the strings and they are not null before they are sent to the service. Any idea why the intent is null? Here is that snippet.

                Intent iniupdates = new Intent(MapActivity.this, InitialLocationUpdater.class);
                iniupdates.setAction("mypackage.InitialLocationUpdater");
                iniupdates.putExtra(InitialLocationUpdater.PARAM_IN_PAS, psgr);
                iniupdates.putExtra(InitialLocationUpdater.PARAM_IN_DRI, tok);
                //iniupdates.putExtra("lat", la);
                //iniupdates.putExtra("lon", lo);
                Log.e("testma600", tok + psgr);
                startService(iniupdates);

Here is the onstartCommand method of the service

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if (null == intent || intent.equals(null) || intent.getData() == null){Log.e("Log", "the intent in onStartCommand is null");}
            else if (intent.getStringExtra(PARAM_IN_PAS) != null && intent.getStringExtra(PARAM_IN_DRI) != null && mLastLocation != null) {
            pass = intent.getStringExtra(PARAM_IN_PAS);
            driv = intent.getStringExtra(PARAM_IN_DRI);
            //String lat = intent.getStringExtra("lat");
            //String lon = intent.getStringExtra("lon");
            sendLocation(driv, String.valueOf(mLastLocation.getLatitude()), String.valueOf(mLastLocation.getLongitude()), pass);
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.e("ERROR", driv +  String.valueOf(mLastLocation.getLatitude()));
        }
        return START_NOT_STICKY;
    }

I also see the following exception on BufferReader line in the stack trace, which I have posted down.

@Override
            protected Void doInBackground(String... args) {
                try {
                    try {
                        URL url;
                        HttpURLConnection urlConn;
                        url = new URL ("serverurl");
                        urlConn = (HttpURLConnection)url.openConnection();
                        urlConn.setDoInput (true);
                        urlConn.setDoOutput (true);
                        urlConn.setUseCaches (false);
                        urlConn.setRequestProperty("Content-Type","application/json");   
                        urlConn.setRequestProperty("Accept", "application/json");
                        urlConn.setChunkedStreamingMode(0);
                        urlConn.setRequestMethod("POST");
                        urlConn.connect();

                        //Create JSONObject here
                        JSONObject json = new JSONObject();
                        json.put("drtoken", args[0]);
                        json.put("drlat", args[1]);
                        json.put("drlon", args[2]);

                        String postData=json.toString();

                        // Send POST output.
                        OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
                          os.write(postData);
                          Log.i("NOTIFICATION", "Data Sent");
                          os.close();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
                        String msg=""; 
                        String line = ""; 
                        while ((line = reader.readLine()) != null) {
                            msg += line; } 
                        Log.i("msg=",""+msg);
                    } catch (MalformedURLException muex) {
                        // TODO Auto-generated catch block
                        muex.printStackTrace();
                    } catch (IOException ioex){
                        ioex.printStackTrace();
                    } 
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("ERROR", "There is error in this code " + String.valueOf(args[0]));

                }
                return null;
            }

Here is the stack trace.

12-31 13:30:49.627 12185-12451/mypackage  W/System.err: java.net.ConnectException: failed to connect to /192.168.0.44 (port 80): connect failed: ETIMEDOUT (Connection timed out)
12-31 13:30:49.638 12185-12451/mypackage  W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:114)
12-31 13:30:49.638 12185-12451/mypackage  W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-31 13:30:49.638 12185-12451/mypackage  W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
12-31 13:30:49.641 12185-12451/mypackage  W/System.err:     at java.net.Socket.connect(Socket.java:833)
12-31 13:30:49.641 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
12-31 13:30:49.642 12185-12451/mypackage  W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
12-31 13:30:49.643 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
12-31 13:30:49.643 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-31 13:30:49.644 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-31 13:30:49.644 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-31 13:30:49.645 12185-12451/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-31 13:30:49.648 12185-12451/mypackage  W/System.err:     at mypackage .MapActivity$PostData.doInBackground(MapActivity.java:1241)
12-31 13:30:49.649 12185-12451/mypackage  W/System.err:     at mypackage .MapActivity$PostData.doInBackground(MapActivity.java:1223)
12-31 13:30:49.650 12185-12451/mypackage  W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-31 13:30:49.651 12185-12451/mypackage  W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-31 13:30:49.652 12185-12451/mypackage  W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-31 13:30:49.652 12185-12451/mypackage  W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-31 13:30:49.653 12185-12451/mypackage  W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-31 13:30:49.656 12185-12451/mypackage  W/System.err:     at java.lang.Thread.run(Thread.java:841)
12-31 13:30:49.657 12185-12451/mypackage  W/System.err: Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
12-31 13:30:49.658 12185-12451/mypackage  W/System.err:     at libcore.io.Posix.connect(Native Method)
12-31 13:30:49.662 12185-12451/mypackage  W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
12-31 13:30:49.663 12185-12451/mypackage  W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-31 13:30:49.664 12185-12451/mypackage  W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
12-31 13:30:49.664 12185-12451/mypackage W/System.err:  ... 18 more
The_Martian
  • 3,684
  • 5
  • 33
  • 61
  • How is passed Intent null if shown stacktrace is about "failed to connect to /192.168.0.44 (port 80): connect failed: ETIMEDOUT (Connection timed out)" ? – Marek Sebera Dec 31 '15 at 22:09

2 Answers2

1

The stack trace is say that URL is invalid! and connect failed (Connection timed out).

In your code, you handled null intent.

 if (null == intent || intent.equals(null) || intent.getData() == null){Log.e("Log", "the intent in onStartCommand is null");}

in first step please check URL. To send data to your service, change create intent like this :

Intent iniupdates = new Intent(MapActivity.this, InitialLocationUpdater.class);
Bundle bundle = new Bundle();        
bundle.putString(InitialLocationUpdater.PARAM_IN_PAS, psgr);        
bundle.putString(InitialLocationUpdater.PARAM_IN_DRI, tok);   

iniupdates.putExtras(bundle);
startService(iniupdates);

In your service use bellow code to retrieve data:

Bundle bundle = intent.getExtras();

if (bundle != null)
{
   String psgr = bundle.getString(PARAM_IN_PAS);
   String tok = bundle.getString(PARAM_IN_DRI);
}
Fred
  • 3,365
  • 4
  • 36
  • 57
  • I would like to start the service from the mapactivity in which the asyncTask is nested in. The service class is in a separate java file. The asyncTask here has nothing to do with starting the service. – The_Martian Dec 31 '15 at 22:42
  • I appreciate your help. However, I am still getting null intent. I guess it has something to do with my other app sending data, hence firing the asyncTask which has already timedout. It is more complex than my snippets. I am working out on it and I will update if I resolved it. :) – The_Martian Dec 31 '15 at 23:27
0

Turns out the sendLocation() method was doing long process within the service and as a result the system was throwing networkonmainthread exception, which was an underlying issue based on the accepted answer in this SO question,

I created a new AsyncTask class nested in the service class and did all my long operations in there.

Now I don't have null intent anymore. I can't understand why though. I never thought that the networkonmainthread exception could have been behind this.

林果皞
  • 7,539
  • 3
  • 55
  • 70
The_Martian
  • 3,684
  • 5
  • 33
  • 61