Android 空字符串从 AsyncTask 传递到 Service

发布于 2024-11-18 02:25:49 字数 2367 浏览 3 评论 0原文

我有一个服务启动 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("&lt;dc:creator&gt;");
            if (first_index == -1) {
                    return null;
            }

            int last_index = mResponse.lastIndexOf("&lt;/dc:creator&gt;");
            String artist = mResponse.substring(first_index + "&lt;dc:creator&gt;".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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2024-11-25 02:25:49

onPostExecute 方法获取从 doInBackground 返回的值作为参数,因此您应该以不同的方式实现这些方法:

protected String doInBackground(Void... params) {
        try {
                //[..]

        } catch (Exception e) {
                return e.getMessage();
        }
        return result;
}

protected void onPostExecute(String result) {
        mListener.onHttpResponse(result, REQUEST_CODE);
}

您在每个循环步骤中重写结果,并且最终只得到文档的最后一行:

 while ((line = rd.readLine()) != null) {
     result = line;
 }

The onPostExecute methods gets the values returned from doInBackground as arguments so you should implement those methods differently:

protected String doInBackground(Void... params) {
        try {
                //[..]

        } catch (Exception e) {
                return e.getMessage();
        }
        return result;
}

protected void onPostExecute(String result) {
        mListener.onHttpResponse(result, REQUEST_CODE);
}

You are overriding the result in every loop step, and you end only up with the last line of the document:

 while ((line = rd.readLine()) != null) {
     result = line;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文