简体   繁体   English

如何获取AsyncHttpClient的onSucces值?

[英]How to get AsyncHttpClient onSucces value?

I have an AsyncHttpClient that makes a get request to an url. 我有一个AsyncHttpClient,它对URL进行了获取请求。 I will take the response of the method onSuccess(). 我将采用onSuccess()方法的响应。 I paste my code to a better explain.. 我将代码粘贴到更好的解释中。

CategoryActivity.java call method getXML of class "XMLParser". 类“ XMLParser”的CategoryActivity.java调用方法getXML。 In this method it is the AsyncHttpClient. 在此方法中,它是AsyncHttpClient。

    XMLParser parser = new XMLParser(URL);
    xml = parser.getXML(URL);

XMLParser.java has .getXML(url) and return a String. XMLParser.java具有.getXML(url)并返回一个String。 I create a variable xmlResponse to put response value but it doesn't. 我创建了一个变量xmlResponse来放置响应值,但是没有。

    public String getXML(String url) {
    // get connection..
    String xmlResponse = "";

    AsyncHttpClient client = new AsyncHttpClient();
    client.get(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            super.onSuccess(response);
            xmlResponse = response;
        }
        @Override
        public void onFailure(Throwable e) {
            ma.showUserAlertQuestion();
        }
    });
    return xmlResponse;
}

How can I get back this value? 如何获得该值? Thank you in advance & sorry for my bad english. 在此先感谢您,对不起我的英语不好。

This is actually a race condition. 这实际上是一个竞赛条件。 The reason why you cannot get the value of xmlResponse is because your HTTP request is an asynchronous method, the result returned before it gets back the response from the server. 之所以无法获取xmlResponse的值,是因为您的HTTP请求是异步方法,因此返回的结果要先从服务器获取响应。

This answer basically explains everything, to get the response value of onSuccess() , you need to use an interface: 这个答案基本上解释了一切,要获得onSuccess()的响应值,您需要使用一个接口:

public Interface XMLCallback {
    onSuccess(String response);
}

In your parser class: 在您的解析器类中:

public String getXML(String url, XMLCallback callback) {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            super.onSuccess(response);
            callback.onSuccess(response);
        }
        @Override
        public void onFailure(Throwable e) {
            ma.showUserAlertQuestion();
        }
    });
}

And in your activity class: 在您的活动课中:

private String XMLResponse = "";

XMLParser parser = new XMLParser(URL);
xml = parser.getXML(URL, new XMLCallback() {
    @Override
    public void onSuccess(String response) {
        XMLResponse = response;
    }
});

I've got the same problem of you in other context. 在其他情况下,我也遇到了同样的问题。 You can see it here: https://stackoverflow.com/questions/21578458/return-asynch-data-to-method-android-java I am also searching for a solution, but didn't find it at this moment. 您可以在这里看到它: https : //stackoverflow.com/questions/21578458/return-asynch-data-to-method-android-java我也在寻找解决方案,但目前没有找到它。

The reason why you get no results in your return value, is simple. 您的返回值没有任何结果的原因很简单。 When you do: client.get() the message is send to the server. 执行以下操作时:client.get()将消息发送到服务器。 Than your code goes inmediatly to return xmlResponse. 比您的代码直接返回xmlResponse。 The onSucces method will be called after the return, this is because the software reaches faster the return statement than the message from the Async call is returned by the server. 返回之后将调用onSucces方法,这是因为与服务器从Async调用返回的消息相比,软件到达return语句的速度更快。

In the topic I showed you above is a solution for it. 在上面我向您展示的主题中,这是一个解决方案。 But that solution didn't fit for the way I planned my application. 但是该解决方案不适合我计划应用程序的方式。 Maybe it will fit at your application. 也许它将适合您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM