我有一个在后台运行的服务。它在清单中注册。当我从我的活动中传递几个字符串并启动它时,意图为空。我检查了字符串,在将它们发送到服务之前它们不是null。知道为什么意图是空的吗? 这是片段。
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);
这是服务的onstartCommand方法
@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;
}
我也在堆栈跟踪的BufferReader行上看到以下异常,我已经发布了它。
@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;
}
这是堆栈跟踪。
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
答案 0 :(得分:1)
堆栈跟踪说URL无效!并且连接失败(连接超时)。
在您的代码中,您处理了null intent。
if (null == intent || intent.equals(null) || intent.getData() == null){Log.e("Log", "the intent in onStartCommand is null");}
首先请检查网址。 要将数据发送到您的服务,请更改创建意图,如下所示:
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);
在您的服务中使用下面的代码来检索数据:
Bundle bundle = intent.getExtras();
if (bundle != null)
{
String psgr = bundle.getString(PARAM_IN_PAS);
String tok = bundle.getString(PARAM_IN_DRI);
}
答案 1 :(得分:0)
原来sendLocation()
方法在服务中进行了长时间的处理,因此系统抛出了networkonmainthread
异常,这是基于the accepted answer in this SO question的基础问题,
我创建了一个嵌套在服务类中的新AsyncTask
类,并在那里执行了所有长时间的操作。
现在我不再有null意图了。我无法理解为什么。我从未想过networkonmainthread
例外可能是背后的。