Android 空字符串从 AsyncTask 传递到 Service
我有一个服务启动 AsyncTask 以从我的家庭网络中的设备检索一些数据。当 AsyncTask 完成时,它使用服务实现的 onPostExecute 中的回调接口将读取的数据传递回服务。然后,该服务读取传递的字符串中的信息,并根据该信息执行操作。这在 9/10 次中效果很好,但有时从 asynctask 传递的字符串为 null。
AsyncTask doInBackground:
private String result = "No results";
@Override
protected Void doInBackground(Void... params) {
try {
OutputStreamWriter wr = new OutputStreamWriter(mConnection.getOutputStream());
wr.write(body);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(mConnection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result = line;
}
wr.close();
rd.close();
} catch (Exception e) {
result = e.getMessage();
}
return null;
}
protected void onPostExecute(Void result) {
mListener.onHttpResponse(this.result, REQUEST_CODE);
}
}
Service onHttpResponse:
@Override
public void onHttpResponse(String response, int REQUEST_CODE) {
ResponseEnterpreter mEnterpreter = new ResponseEnterpreter(response);
switch (REQUEST_CODE) {
case WidgetConstants.REQUEST_LED_STATE :
String artist = mEnterpreter.getArtist(); <--- NullPointerException
ResponseEnterpreter getArtist:
public class ResponseEnterpreter {
private String mResponse;
public ResponseEnterpreter(String mResponse) {
this.mResponse = mResponse;
}
public String getArtist() {
int first_index = mResponse.lastIndexOf("<dc:creator>");
if (first_index == -1) {
return null;
}
int last_index = mResponse.lastIndexOf("</dc:creator>");
String artist = mResponse.substring(first_index + "<dc:creator>".length(), last_index);
return unEscapeString(artist);
}
据我了解,结果字符串被初始化为“No results”,如果 BufferedReader 没有检索到结果,“No Result”就是应该传递的字符串。 mConnection 设置为 3 秒超时。
我真的对此摸不着头脑......
有什么想法吗?
// 弗雷德里克
I have a Service that launches an AsyncTask to retrive some data from a device in my homenetwork. When the AsyncTask is done it passes the data read back to the Service using a callback-interface in onPostExecute that the Service implements. The service then reads the information in the passed String and does stuff depending on the information. This works great 9/10 times, but sometimes the String passed from the asynctask is null.
AsyncTask doInBackground:
private String result = "No results";
@Override
protected Void doInBackground(Void... params) {
try {
OutputStreamWriter wr = new OutputStreamWriter(mConnection.getOutputStream());
wr.write(body);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(mConnection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result = line;
}
wr.close();
rd.close();
} catch (Exception e) {
result = e.getMessage();
}
return null;
}
protected void onPostExecute(Void result) {
mListener.onHttpResponse(this.result, REQUEST_CODE);
}
}
Service onHttpResponse:
@Override
public void onHttpResponse(String response, int REQUEST_CODE) {
ResponseEnterpreter mEnterpreter = new ResponseEnterpreter(response);
switch (REQUEST_CODE) {
case WidgetConstants.REQUEST_LED_STATE :
String artist = mEnterpreter.getArtist(); <--- NullPointerException
ResponseEnterpreter getArtist:
public class ResponseEnterpreter {
private String mResponse;
public ResponseEnterpreter(String mResponse) {
this.mResponse = mResponse;
}
public String getArtist() {
int first_index = mResponse.lastIndexOf("<dc:creator>");
if (first_index == -1) {
return null;
}
int last_index = mResponse.lastIndexOf("</dc:creator>");
String artist = mResponse.substring(first_index + "<dc:creator>".length(), last_index);
return unEscapeString(artist);
}
To my understanding, the result string is initialized as "No results" and if no result is retrived by the BufferedReader "No Result" is the String that should be passed along. The mConnection is set to 3sec timeout.
I'm really scratching my head over this....
Any ideas?
// Fredrik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onPostExecute 方法获取从 doInBackground 返回的值作为参数,因此您应该以不同的方式实现这些方法:
您在每个循环步骤中重写结果,并且最终只得到文档的最后一行:
The onPostExecute methods gets the values returned from doInBackground as arguments so you should implement those methods differently:
You are overriding the result in every loop step, and you end only up with the last line of the document: